-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay23.c
428 lines (347 loc) · 11.2 KB
/
Day23.c
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include "Helpers.c"
#define QUEUE_CAP 200000
typedef struct {
int x;
int y;
} Amp;
typedef struct {
int32_t energy;
int8_t m[5][11];
} Map;
static Map queue[QUEUE_CAP];
static int nQueue = 0;
static int iQueue = 0;
#define INDEX_CAP 0x3fff
#define BUCKET_CAP 800
static int visitedTable[INDEX_CAP + 1][BUCKET_CAP];
static int16_t nVisitedTable[INDEX_CAP + 1] = {0};
static Map parse(const char *input, bool part2) {
int charsRead = 0;
char a1[2] = {0};
char a2[2] = {0};
char a3[2] = {0};
char a4[2] = {0};
Map map = {0};
sscanf(input, "#############\n%n", &charsRead);
input += charsRead;
sscanf(input, "#...........#\n%n", &charsRead);
input += charsRead;
assert(sscanf(input, "###%1[^#]#%1[^#]#%1[^#]#%1[^#]###\n%n", a1, a2, a3, a4, &charsRead) == 4);
input += charsRead;
map.m[1][2] = a1[0];
map.m[1][4] = a2[0];
map.m[1][6] = a3[0];
map.m[1][8] = a4[0];
if (part2) {
// #D#C#B#A#
map.m[2][2] = 'D';
map.m[2][4] = 'C';
map.m[2][6] = 'B';
map.m[2][8] = 'A';
// #D#B#A#C#
map.m[3][2] = 'D';
map.m[3][4] = 'B';
map.m[3][6] = 'A';
map.m[3][8] = 'C';
}
int y = part2 ? 4 : 2;
assert(sscanf(input, " #%1[^#]#%1[^#]#%1[^#]#%1[^#]#\n%n", a1, a2, a3, a4, &charsRead) == 4);
input += charsRead;
map.m[y][2] = a1[0];
map.m[y][4] = a2[0];
map.m[y][6] = a3[0];
map.m[y][8] = a4[0];
return map;
}
static void dump(int roomSize, const Map *map) {
for (int y = 0; y <= roomSize; ++y) {
for (int x = 0; x < 11; ++x) {
printf("%c", map->m[y][x] == 0 ? ' ' : map->m[y][x]);
}
printf("\n");
}
}
static int findMalplacedAmps(int roomSize, const Map *map, Amp amps[16]) {
int n = 0;
for (int x = 0; x < 11; ++x) {
if (map->m[0][x] != 0) {
assert(n < 16);
amps[n].x = x;
amps[n].y = 0;
++n;
}
}
for (int y = roomSize; y >= 1; --y) {
if (map->m[y][2] != 0 && map->m[y][2] != 'A') {
for (int ay = y; ay >= 1; --ay) {
if (map->m[ay][2] != 0) {
assert(n < 16);
amps[n].x = 2;
amps[n].y = ay;
++n;
}
}
break;
}
}
for (int y = roomSize; y >= 1; --y) {
if (map->m[y][4] != 0 && map->m[y][4] != 'B') {
for (int by = y; by >= 1; --by) {
if (map->m[by][4] != 0) {
assert(n < 16);
amps[n].x = 4;
amps[n].y = by;
++n;
}
}
break;
}
}
for (int y = roomSize; y >= 1; --y) {
if (map->m[y][6] != 0 && map->m[y][6] != 'C') {
for (int by = y; by >= 1; --by) {
if (map->m[by][6] != 0) {
assert(n < 16);
amps[n].x = 6;
amps[n].y = by;
++n;
}
}
break;
}
}
for (int y = roomSize; y >= 1; --y) {
if (map->m[y][8] != 0 && map->m[y][8] != 'D') {
for (int by = y; by >= 1; --by) {
if (map->m[by][8] != 0) {
assert(n < 16);
amps[n].x = 8;
amps[n].y = by;
++n;
}
}
break;
}
}
return n;
}
static int moveOnce(int roomSize, const Amp amp, const Map *map, Map outMaps[10]) {
int n = 0;
int x = amp.x;
int y = amp.y;
char t = map->m[y][x];
assert(t != 0);
int destX =
t == 'A' ? 2
: t == 'B' ? 4
: t == 'C' ? 6
: 8;
int energyPerStep =
t == 'A' ? 1
: t == 'B' ? 10
: t == 'C' ? 100
: 1000;
if (y > 0) { // Is inside a room. Can we go to the hallway?
int ySteps = 0;
for (int y2 = y - 1; y2 >= 0; --y2) {
if (map->m[y2][x] != 0) {
ySteps = 0;
break;
}
++ySteps;
}
if (ySteps > 0) { // Yes, can we walk past the hallway to the correct room?
bool canWalkToHallway = true;
if (destX > x) {
for (int x2 = x + 1; x2 <= destX; ++x2) {
if (map->m[0][x2] != 0) {
canWalkToHallway = false;
break;
}
}
} else {
for (int x2 = x - 1; x2 >= destX; --x2) {
if (map->m[0][x2] != 0) {
canWalkToHallway = false;
break;
}
}
}
if (canWalkToHallway) { // Yes, can we enter the room?
int roomY = 0;
for (int y2 = roomSize; y2 >= 1; --y2) {
if (map->m[y2][destX] == 0) {
roomY = y2;
break;
} else if (map->m[y2][destX] == t) {
continue;
} else {
break;
}
}
if (roomY > 0) { // Yes, move into the room.
outMaps[n] = *map;
outMaps[n].energy += energyPerStep * (abs(destX - x) + ySteps + roomY);
outMaps[n].m[y][x] = 0;
outMaps[n].m[roomY][destX] = t;
++n;
assert(n < 10);
return n;
}
}
// Nope, collect valid positions in the hallway.
for (int x2 = x - 1; x2 >= 0; --x2) {
if (map->m[0][x2] != 0) {
break;
}
if (x2 == 2 || x2 == 4 || x2 == 6 || x2 == 8) {
continue;
}
outMaps[n] = *map;
outMaps[n].energy += energyPerStep * (abs(x2 - x) + ySteps);
outMaps[n].m[y][x] = 0;
outMaps[n].m[0][x2] = t;
++n;
assert(n < 10);
}
for (int x2 = x + 1; x2 < 11; ++x2) {
if (map->m[0][x2] != 0) {
break;
}
if (x2 == 2 || x2 == 4 || x2 == 6 || x2 == 8) {
continue;
}
outMaps[n] = *map;
outMaps[n].energy += energyPerStep * (abs(x2 - x) + ySteps);
outMaps[n].m[y][x] = 0;
outMaps[n].m[0][x2] = t;
++n;
assert(n < 10);
}
}
} else { // In the hallway. Can we walk to the correct room hallway?
bool canWalkToRoomHallway = true;
if (x < destX) {
for (int x2 = x + 1; x2 <= destX; ++x2) {
if (map->m[0][x2] != 0) {
canWalkToRoomHallway = false;
break;
}
}
} else {
for (int x2 = x - 1; x2 >= destX; --x2) {
if (map->m[0][x2] != 0) {
canWalkToRoomHallway = false;
break;
}
}
}
if (canWalkToRoomHallway) { // Yes, can we enter the room?
int roomY = 0;
for (int y2 = roomSize; y2 >= 1; --y2) {
if (map->m[y2][destX] == 0) {
roomY = y2;
break;
} else if (map->m[y2][destX] == t) {
continue;
} else {
break;
}
}
if (roomY > 0) { // Yes, move into the room
outMaps[n] = *map;
outMaps[n].energy += energyPerStep * (abs(destX - x) + roomY);
outMaps[n].m[y][x] = 0;
outMaps[n].m[roomY][destX] = t;
++n;
assert(n < 10);
}
}
}
return n;
}
static bool isMapEqual(int roomSize, const Map *a, const Map *b) {
for (int y = 0; y <= roomSize; ++y) {
for (int x = 0; x < 11; ++x) {
if (a->m[y][x] != b->m[y][x]) {
return false;
}
}
}
return true;
}
// I winged this one, did some checks on distributions. Look ok for my input.
static inline uint64_t mapHash(const int8_t m[5][11]) {
return (((uint64_t)(m[1][2]) & 3) << 0) |
(((uint64_t)(m[1][4]) & 3) << 1) |
(((uint64_t)(m[1][6]) & 3) << 2) |
(((uint64_t)(m[1][8]) & 3) << 3) |
(((uint64_t)(m[2][2]) & 3) << 4) |
(((uint64_t)(m[2][4]) & 3) << 5) |
(((uint64_t)(m[2][6]) & 3) << 6) |
(((uint64_t)(m[2][8]) & 3) << 7) |
(((uint64_t)(m[0][0]) & 3) << 8) |
(((uint64_t)(m[0][1]) & 3) << 9) |
(((uint64_t)(m[0][3]) & 3) << 10) |
(((uint64_t)(m[0][5]) & 3) << 11) |
(((uint64_t)(m[0][7]) & 3) << 13);
}
static int partOne(int roomSize, const Map *map) {
int minEnergy = INT_MAX;
Amp amps[16] = {0};
Map maps[10] = {0};
iQueue = 0;
nQueue = 0;
queue[nQueue++] = *map;
memset(nVisitedTable, 0, sizeof(nVisitedTable));
while (iQueue < nQueue) {
Map *m = &queue[iQueue++];
int nAmps = findMalplacedAmps(roomSize, m, amps);
if (nAmps == 0) {
dump(roomSize, m);
if (m->energy < minEnergy) {
minEnergy = m->energy;
}
}
for (int i = 0; i < nAmps; ++i) {
int nMaps = moveOnce(roomSize, amps[i], m, maps);
for (int j = 0; j < nMaps; ++j) {
if (maps[j].energy < minEnergy) {
uint64_t hash = mapHash(maps[j].m);
int hashIndex = hash & INDEX_CAP;
int visitedMap = -1;
for (int k = 0; k < nVisitedTable[hashIndex]; ++k) {
int queueIndex = visitedTable[hashIndex][k];
if (isMapEqual(roomSize, &queue[queueIndex], &maps[j])) {
visitedMap = queueIndex;
break;
}
}
if (visitedMap >= 0) {
if (queue[visitedMap].energy > maps[j].energy) {
queue[visitedMap].energy = maps[j].energy;
}
} else {
assert(nQueue < QUEUE_CAP);
assert(nVisitedTable[hashIndex] < BUCKET_CAP);
visitedTable[hashIndex][nVisitedTable[hashIndex]++] = nQueue;
queue[nQueue++] = maps[j];
}
}
}
}
}
return minEnergy;
}
int main() {
const char *input = Helpers_readInputFile(__FILE__);
Map mapPart1 = parse(input, false);
Helpers_assert(PART1, Helpers_clock(),
partOne(2, &mapPart1),
12521, 14350);
Map mapPart2 = parse(input, true);
Helpers_assert(PART2, Helpers_clock(),
partOne(4, &mapPart2),
44169, 49742);
return 0;
}