-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15.odin
276 lines (240 loc) · 5.89 KB
/
day15.odin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package day15
import "core:fmt"
import "core:os"
import "core:slice"
import "core:strconv"
import "core:strings"
import "core:text/regex"
main :: proc() {
content, ok := os.read_entire_file("input/day15.txt")
if !ok {
// could not read file
return
}
defer delete(content)
lines := strings.split_lines(string(content))
arr := make([]rune, len(strings.join(lines, "")))
moves := make([dynamic]rune)
map_input := true
map_height := 0
for line, y in lines {
if len(line) == 0 {
map_input = false
}
if map_input {
for char, x in line {
arr[y * len(lines[0]) + x] = char
}
map_height += 1
} else {
for char in line {
append(&moves, char)
}
}
}
source_warehouse := Array2D(rune){arr, len(lines[0]), map_height}
// fmt.println(warehouse)
// fmt.println(moves)
warehouse1 := copy(source_warehouse)
for move in moves {
pos, _ := find(warehouse1, '@')
ok := false
switch move {
case '>':
ok = attempt_move(warehouse1, pos, Vec2{+1, 0})
case 'v':
ok = attempt_move(warehouse1, pos, Vec2{0, +1})
case '<':
ok = attempt_move(warehouse1, pos, Vec2{-1, 0})
case '^':
ok = attempt_move(warehouse1, pos, Vec2{0, -1})
}
// print(warehouse1)
}
// Part 1
fmt.println(box_gps_sum(warehouse1))
warehouse2 := scale(source_warehouse)
for move in moves {
pos, _ := find(warehouse2, '@')
ok := false
switch move {
case '>':
ok = attempt_move(warehouse2, pos, Vec2{+1, 0})
case 'v':
ok = attempt_move(warehouse2, pos, Vec2{0, +1})
case '<':
ok = attempt_move(warehouse2, pos, Vec2{-1, 0})
case '^':
ok = attempt_move(warehouse2, pos, Vec2{0, -1})
}
// print(warehouse2)
}
// Part 2
fmt.println(box_gps_sum(warehouse2))
}
Array2D :: struct($T: typeid) {
arr: []T,
width: int,
height: int,
}
Vec2 :: struct {
x: int,
y: int,
}
get :: proc(arr: Array2D($T), pos: Vec2) -> (T, bool) {
if pos.x < 0 || pos.x >= arr.width || pos.y < 0 || pos.y >= arr.height {
return ' ', false
}
return arr.arr[pos.y * arr.width + pos.x], true
}
set :: proc(arr: Array2D($T), pos: Vec2, v: T) -> bool {
if pos.x < 0 || pos.x >= arr.width || pos.y < 0 || pos.y >= arr.height {
return false
}
arr.arr[pos.y * arr.width + pos.x] = v
return true
}
find :: proc(arr: Array2D($T), x: T) -> (Vec2, bool) {
for ax, i in arr.arr {
if ax == x {
return Vec2{i % arr.width, i / arr.width}, true
}
}
return Vec2{-1, -1}, false
}
print :: proc(arr: Array2D($T)) {
for y in 0 ..< arr.height {
for x in 0 ..< arr.width {
fmt.print(get(arr, Vec2{x, y}) or_else '?')
}
fmt.println()
}
}
copy :: proc(arr: Array2D($T)) -> Array2D(T) {
arr_arr_copy := make([]T, len(arr.arr))
copy_slice(arr_arr_copy[:], arr.arr[:])
return Array2D(T){arr_arr_copy, arr.width, arr.height}
}
scale :: proc(arr: Array2D($T)) -> Array2D(T) {
new_arr := make([]T, len(arr.arr) * 2)
for a, i in arr.arr {
switch a {
case '#':
new_arr[i * 2] = '#'
new_arr[i * 2 + 1] = '#'
case '.':
new_arr[i * 2] = '.'
new_arr[i * 2 + 1] = '.'
case 'O':
new_arr[i * 2] = '['
new_arr[i * 2 + 1] = ']'
case '@':
new_arr[i * 2] = '@'
new_arr[i * 2 + 1] = '.'
}
}
return Array2D(T){new_arr, arr.width * 2, arr.height}
}
attempt_move :: proc(area: Array2D($T), pos: Vec2, dir: Vec2) -> bool {
all_positions := make([dynamic]Vec2)
unchecked_positions := make([dynamic]Vec2)
append(&unchecked_positions, pos)
for len(unchecked_positions) > 0 {
curr := pop(&unchecked_positions)
next := Vec2{curr.x + dir.x, curr.y + dir.y}
curr_t := get(area, curr) or_else '?'
next_t := get(area, next) or_else '?'
if curr_t == '.' {
continue
}
if next_t == '#' {
inject_at(&unchecked_positions, 0, next)
break
}
if curr_t == '@' {
append(&all_positions, curr)
if dir.y == 0 || next_t == 'O' {
inject_at(&unchecked_positions, 0, next)
} else {
if next_t == '[' {
inject_at(&unchecked_positions, 0, next)
inject_at(&unchecked_positions, 0, Vec2{next.x + 1, next.y})
} else if next_t == ']' {
inject_at(&unchecked_positions, 0, next)
inject_at(&unchecked_positions, 0, Vec2{next.x - 1, next.y})
}
}
continue
}
if curr_t == ']' && next_t == '[' {
append(&all_positions, curr)
if dir.y == 0 {
inject_at(&unchecked_positions, 0, next)
} else {
inject_at(&unchecked_positions, 0, next)
inject_at(&unchecked_positions, 0, Vec2{next.x + 1, next.y})
}
continue
}
if curr_t == '[' && next_t == ']' {
append(&all_positions, curr)
if dir.y == 0 {
inject_at(&unchecked_positions, 0, next)
} else {
inject_at(&unchecked_positions, 0, next)
inject_at(&unchecked_positions, 0, Vec2{next.x - 1, next.y})
}
continue
}
if curr_t == next_t {
append(&all_positions, curr)
if dir.y == 0 || curr_t == 'O' {
inject_at(&unchecked_positions, 0, next)
} else {
if curr_t == '[' {
inject_at(&unchecked_positions, 0, next)
inject_at(&unchecked_positions, 0, Vec2{next.x + 1, next.y})
} else if curr_t == ']' {
inject_at(&unchecked_positions, 0, next)
inject_at(&unchecked_positions, 0, Vec2{next.x - 1, next.y})
}
}
continue
}
if next_t == '.' {
append(&all_positions, curr)
inject_at(&unchecked_positions, 0, next)
continue
}
}
if len(unchecked_positions) == 0 {
// Move possible.
slice.reverse(all_positions[:])
for curr in all_positions {
next := Vec2{curr.x + dir.x, curr.y + dir.y}
curr_t := get(area, curr) or_else '?'
next_t := get(area, next) or_else '?'
if curr_t == '.' {
// Nothing to actually move (already moved).
continue
}
set(area, next, curr_t)
set(area, curr, '.')
}
return true
} else {
return false
}
}
box_gps_sum :: proc(warehouse: Array2D($T)) -> int {
result := 0
for y in 0 ..< warehouse.height {
for x in 0 ..< warehouse.width {
v := get(warehouse, Vec2{x, y}) or_else '?'
if v == 'O' || v == '[' {
result += y * 100 + x
}
}
}
return result
}