-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday22.c3
353 lines (337 loc) · 6.73 KB
/
day22.c3
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
module day22;
import std::io;
import std::math;
const RIGHT = 0;
const DOWN = 1;
const LEFT = 2;
const UP = 3;
enum MapType : char
{
VOID,
SPACE,
BLOCK
}
int[200][200] map;
String commands;
int[<2>] start = { -1, -1 };
int side;
int width;
int height;
int width_spaces;
fn void load_map()
{
File f = file::open("map.txt", "rb")!!;
defer (void)f.close();
int y = 0;
width = 0;
while (!f.eof())
{
@pool()
{
String line = io::treadline(&f)!!;
if (line.len > width) width = line.len;
if (!line.len) break;
foreach (int x, c : line)
{
switch (c)
{
case '#':
map[x][y] = (int)MapType.BLOCK;
case '.':
if (start[0] < 0)
{
start = { x, y };
}
map[x][y] = (int)MapType.SPACE;
case ' ':
map[x][y] = (int)MapType.VOID;
default:
unreachable();
}
}
y++;
};
}
height = y;
side = y > width ? y / 4 : width / 4;
width_spaces = width / side;
commands = io::treadline(&f)!!.copy();
}
fn int region_from_pos(int[<2>] pos)
{
int x_region = pos[0] / side;
int y_region = pos[1] / side;
return y_region * width_spaces + x_region;
}
fn int[<2>] region_start(int region)
{
return { (region % width_spaces) * side, (region / width_spaces ) * side };
}
fn int[<2>] update(int[<2>] pos, int* facing_ref, int to_facing, int region)
{
int facing = *facing_ref;
*facing_ref = to_facing;
int[<2>] rel_pos = pos % int[<2>] { side, side };
bool reverse = (to_facing - facing + 4) % 4 == 2; // Reverse on UP <-> DOWN LEFT <-> RIGHT
if (reverse) rel_pos = int[<2>] { side - 1, side - 1 } - rel_pos;
int[<2>] res;
int val;
switch (facing)
{
case LEFT:
case RIGHT:
val = rel_pos[1];
case DOWN:
case UP:
val = rel_pos[0];
}
switch (to_facing)
{
case DOWN: res = { val, 0 };
case RIGHT: res = { 0, val };
case LEFT: res = { side - 1, val };
case UP: res = { val, side - 1 };
}
return region_start(region) + res;
}
// This wrapping only works for the particular layout of the cube in the example.
fn void wrap(int[<2>]* pos_ref, int* facing)
{
int[<2>] pos = *pos_ref;
switch (region_from_pos(pos))
{
case 1:
switch (*facing)
{
case RIGHT:
unreachable();
case DOWN:
unreachable();
case UP:
*pos_ref = update(pos, facing, RIGHT, 9);
case LEFT:
*pos_ref = update(pos, facing, RIGHT, 6);
}
case 2:
switch (*facing)
{
case LEFT:
unreachable();
case RIGHT:
*pos_ref = update(pos, facing, LEFT, 7);
case UP:
*pos_ref = update(pos, facing, UP, 9);
case DOWN:
*pos_ref = update(pos, facing, LEFT, 4);
}
case 4:
switch (*facing)
{
case LEFT:
*pos_ref = update(pos, facing, DOWN, 6);
case RIGHT:
*pos_ref = update(pos, facing, UP, 2);
case DOWN:
unreachable();
case UP:
unreachable();
}
case 6:
switch (*facing)
{
case LEFT:
*pos_ref = update(pos, facing, RIGHT, 1);
case RIGHT:
unreachable();
case DOWN:
unreachable();
case UP:
*pos_ref = update(pos, facing, RIGHT, 4);
}
case 7:
switch (*facing)
{
case LEFT:
unreachable();
case UP:
unreachable();
case RIGHT:
*pos_ref = update(pos, facing, LEFT, 2);
case DOWN:
*pos_ref = update(pos, facing, LEFT, 9);
}
case 9:
switch (*facing)
{
case DOWN:
*pos_ref = update(pos, facing, DOWN, 2);
case RIGHT:
*pos_ref = update(pos, facing, UP, 7);
case UP:
unreachable();
case LEFT:
*pos_ref = update(pos, facing, DOWN, 1);
}
default:
unreachable();
}
}
fn int[<2>] facing_diff(int facing)
{
switch (facing)
{
case RIGHT: return { 1, 0 };
case DOWN: return { 0, 1 };
case LEFT: return { -1, 0 };
case UP: return { 0, -1 };
default: unreachable();
}
}
macro MapType map_at(int[<2>] pos)
{
return (MapType)map[pos[0]][pos[1]]; // Bug with cast.
}
fn void part1()
{
int[<2>] pos = start;
int len = commands.len;
int index = 0;
int facing = 0;
while (index < len)
{
int steps = -1;
int new_facing @noinit;
for (int i = index; i < len; i++)
{
if (commands[i] >= 'A')
{
steps = commands[index..(i - 1)].to_int()!!;
new_facing = commands[i] == 'R' ? 1 : -1;
index = i + 1;
break;
}
}
if (steps == -1)
{
new_facing = 4;
steps = commands[index..].to_int()!!;
index = len;
}
int[<2>] move = facing_diff(facing);
for WALK: (int i = 0; i < steps; i++)
{
int[<2>] old = pos;
pos += move;
if (pos[0] == width) pos[0] = 0;
if (pos[1] == height) pos[1] = 0;
if (pos[0] == -1) pos[0] = width - 1;
if (pos[1] == -1) pos[1] = height - 1;
switch (map_at(pos))
{
case SPACE:
continue WALK;
case VOID:
switch
{
case move[0] > 0: pos[0] = 0;
case move[0] < 0: pos[0] = width - 1;
case move[1] > 0: pos[1] = 0;
case move[1] < 0: pos[1] = height - 1;
}
while (map_at(pos) == MapType.VOID) pos += move;
switch (map_at(pos))
{
case BLOCK:
pos = old;
break WALK;
case SPACE:
continue WALK;
default:
break;
}
unreachable();
case BLOCK:
pos = old;
break WALK;
default:
unreachable();
}
}
facing = (4 + new_facing + facing) % 4;
}
long value = (pos[0] + 1i64) * 4i64 + (pos[1] + 1i64) * 1000i64 + facing;
io::printfn("Password: %d", value);
}
fn void part2()
{
int[<2>] pos = start;
int len = commands.len;
int index = 0;
int facing = 0;
while (index < len)
{
int steps = -1;
int new_facing @noinit;
for (int i = index; i < len; i++)
{
if (commands[i] >= 'A')
{
steps = commands[index..(i - 1)].to_int()!!;
new_facing = commands[i] == 'R' ? 1 : -1;
index = i + 1;
break;
}
}
if (steps == -1)
{
new_facing = 4;
steps = commands[index..].to_int()!!;
index = len;
}
int[<2>] move = facing_diff(facing);
for WALK: (int i = 0; i < steps; i++)
{
int[<2>] old = pos;
pos += move;
if (pos.comp_lt({ 0, 0 }).or() || pos.comp_ge({ width, height}).or() || map_at(pos) == MapType.VOID)
{
pos = old;
int wrap_facing = facing;
wrap(&pos, &wrap_facing);
switch (map_at(pos))
{
case BLOCK:
pos = old;
break WALK;
case SPACE:
facing = wrap_facing;
move = facing_diff(facing);
continue WALK;
default:
break;
}
}
switch (map_at(pos))
{
case SPACE:
continue WALK;
case VOID:
unreachable();
case BLOCK:
pos = old;
break WALK;
default:
unreachable("invalid map");
}
}
facing = (4 + new_facing + facing) % 4;
}
long value = (pos[0] + 1i64) * 4i64 + (pos[1] + 1i64) * 1000i64 + facing;
io::printfn("Password: %d", value);
}
fn void main()
{
load_map();
part1();
part2();
}