-
Notifications
You must be signed in to change notification settings - Fork 2
/
20.c
494 lines (425 loc) · 11.7 KB
/
20.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "inputs/20.h"
#define W 10
#define H 10
enum edge {
EDGE_N,
EDGE_E,
EDGE_S,
EDGE_W,
EDGE_NONE = -1,
};
typedef enum edge edge_t;
enum ax {
AX_X = 'x',
AX_Y = 'y',
};
typedef enum ax ax_t;
enum match {
NO_MATCH,
MATCH,
INVERSE_MATCH
};
typedef enum match match_t;
typedef struct tile tile_t;
struct tile {
int32_t id;
int8_t x;
int8_t y;
char grid[W*H];
};
typedef struct monster monster_t;
const struct monster {
int32_t width;
int32_t height;
const char* body;
} sea_monster = {
.width = 20,
.height = 3,
.body =
""
" # \n"
"# ## ## ###\n"
" # # # # # # ",
};
static int32_t
parse_tiles_from_input(tile_t* restrict tiles) {
int32_t ntiles = 0;
const unsigned char *s = input;
while (*s != '\0') {
tile_t* t = &tiles[ntiles++];
t->x = -1;
t->y = -1;
// first line contains the tile ID: Tile 2311:
s += 5;
t->id = 0;
while (isdigit(*s)) {
t->id = (t->id * 10) + (*s - '0');
s++;
}
s++; // ':'
s++; // '\n'
int32_t y = 0;
while (*s != '\0') {
for (int32_t x = 0; *s != '\n' && *s != '\0'; s++, x++) {
t->grid[y * W + x] = *s;
}
// linebreak: new row
if (*s == '\n') {
s++;
y++;
}
// empty line: new tile
if (*s == '\n') {
s++;
break;
}
}
}
return ntiles;
}
static void
print_tile(const tile_t* restrict tile) {
printf("Tile %d:\n", tile->id);
for (int32_t y = 0; y < H; y++) {
for (int32_t x = 0; x < W; x++) {
printf("%c", tile->grid[y * W + x]);
}
printf("\n");
}
}
__attribute((unused))
static void
print_tiles(const tile_t* restrict tiles, const int32_t ntiles) {
for (int32_t i = 0; i < ntiles; i++) {
print_tile(&tiles[i]);
}
}
// https://en.wikipedia.org/wiki/Cartesian_coordinate_system#Rotation
static void
rotate(char* restrict grid, const int32_t size) {
char new_grid[size * size];
for (int32_t y = 0, x2 = size - 1; y < size; y++, x2--) {
for (int32_t x = 0, y2 = 0; x < size; x++, y2++) {
new_grid[y2 * size + x2] = grid[y * size + x];
}
}
memcpy(grid, new_grid, size * size * sizeof(char));
}
static void
flip(char* restrict grid, const int32_t size, const ax_t ax) {
// 0 1 2 3 4 5 6 7 8 9
// 9 8 7 6 5 4 3 2 1 0
char new_grid[size * size];
for (int32_t y = 0; y < size; y++) {
for (int32_t x = 0; x < size; x++) {
switch (ax) {
case AX_X:
new_grid[y * size + (size - 1 - x)] = grid[y * size + x];
break;
case AX_Y:
new_grid[(size - 1 - y) * size + x] = grid[y * size + x];
break;
}
}
}
memcpy(grid, new_grid, size * size * sizeof(char));
}
static void
extract_edge(const tile_t* restrict t, edge_t e, char dest[W], const bool inverse) {
// extract_edge(tile_t* t, edge_t e, char dest[W]) {
switch (e) {
case EDGE_N:
for (int32_t y=0, x=0; x < W; x++) {
dest[x] = t->grid[y * W + x];
}
break;
case EDGE_E:
for (int32_t y=0, x=W-1; y < H; y++) {
dest[y] = t->grid[y * W + x];
}
break;
case EDGE_S:
for (int32_t y=H-1, x=0; x < W; x++) {
dest[x] = t->grid[y * W + x];
}
break;
case EDGE_W:
for (int32_t y=0, x=0; y < H; y++) {
dest[y] = t->grid[y * W + x];
}
break;
default:
err(EXIT_FAILURE, "can not extract EDGE_NONE");
break;
}
if (inverse) {
char tmp;
for (int32_t i=0, j = W-1; i < j; i++, j--) {
tmp = dest[i];
dest[i] = dest[j];
dest[j] = tmp;
}
}
}
static match_t
cmp_edges(const char edge_1[W], const char edge_2[W]) {
// edges match as-is
if (memcmp(edge_1, edge_2, W * sizeof(char)) == 0) {
return MATCH;
}
// reversed edge matches
int32_t i;
for (i=0; i < W; i++) {
if (edge_1[i] != edge_2[W-1-i]) {
break;
}
}
if (i == W) {
return INVERSE_MATCH;
}
// no match
return NO_MATCH;
}
const bool
fit_other_on_edge(const tile_t* restrict t1, tile_t* restrict t2, const edge_t e1) {
char edge_t1[W];
char edge_t2[W];
extract_edge(t1, e1, edge_t1, false);
edge_t desired_edge = (e1 + 2) % 4;
for (edge_t actual_edge=EDGE_N; actual_edge <= EDGE_W; actual_edge++) {
extract_edge(t2, actual_edge, edge_t2, (desired_edge / 2) != (actual_edge / 2));
match_t match = cmp_edges(edge_t1, edge_t2);
if (match > 0) {
while (actual_edge != desired_edge) {
rotate(t2->grid, W);
actual_edge = (actual_edge + 1) % 4;
}
if (match == INVERSE_MATCH) {
flip(t2->grid, W, desired_edge == EDGE_N || desired_edge == EDGE_S ? AX_X : AX_Y);
}
return true;
}
}
return false;
}
static void
remove_image_borders(char* restrict image, tile_t** const restrict tiles, const int32_t image_size) {
int32_t img_y = 0;
int32_t img_x = 0;
int32_t img_width = (W - 2) * image_size;
for (int32_t image_row = 0; image_row < image_size; image_row++) {
for (int32_t y = 1; y < H - 1; y++) {
for (int32_t ti = 0; ti < image_size; ti++) {
tile_t* t = tiles[image_row * image_size + ti];
for (int32_t x = 1; x < W - 1; x++) {
image[img_y * img_width + img_x++] = t->grid[y * W + x];
}
}
}
}
}
__attribute((unused))
static void
print_image(const tile_t** restrict image, const int32_t image_size) {
for (int32_t image_y = 0; image_y < image_size; image_y++) {
for (int32_t y = 0; y < H; y++) {
for (int32_t image_x = 0; image_x < image_size; image_x++) {
const tile_t* t = image[image_y * image_size + image_x];
for (int32_t x = 0; x < W; x++) {
printf("%c", t != NULL ? t->grid[y * W + x] : '?');
}
}
printf("\n");
}
}
}
__attribute((unused))
static void
print_image_ids(const tile_t** restrict image, const int32_t size) {
for (int32_t y = 0; y < size; y++) {
for (int32_t x = 0; x < size; x++) {
const tile_t* t = image[y * size + x];
printf("%d\t", t != NULL ? t->id : 0);
}
printf("\n");
}
}
static void
shift_image(tile_t** restrict image, const int32_t size, const int8_t shift_y, const int8_t shift_x) {
tile_t* new_image[size * size];
memset(new_image, 0, size * size * sizeof(tile_t*));
tile_t* t;
for (int32_t y = 0; y < size - shift_y; y++) {
for (int32_t x = 0; x < size - shift_x; x++) {
int8_t y2 = y + shift_y;
int8_t x2 = x + shift_x;
t = image[y * size + x];
if (t) {
t->x = x2;
t->y = y2;
new_image[y2 * size + x2] = t;
}
}
}
memcpy(image, new_image, size * size * sizeof(tile_t*));
}
static int32_t
count_sea_monster_in_image(const char* image, const int32_t image_size) {
int32_t count = 0;
for (int32_t y = 0; y < image_size - sea_monster.height; y++) {
for (int32_t x = 0; x < image_size - sea_monster.width; x++) {
const char* i = &image[y * image_size + x];
const char* m = sea_monster.body;
int32_t y_search = y;
int32_t x_search = x;
while (*m == ' ' || *i == *m) {
i++;
m++;
// reached end of monster, it's a match!
if (*m == '\0') {
count++;
}
// monster line matched,
// move to same x position in next row
if (*m == '\n') {
y_search++;
x_search = x;
i = &image[y_search * image_size + x_search];
m++;
}
}
}
}
return count;
}
int day20(void) {
tile_t* tiles = (tile_t*)malloc(200 * sizeof(tile_t));
if (!tiles) {
err(EXIT_FAILURE, "could not allocate memory for tiles");
}
int32_t ntiles = parse_tiles_from_input(tiles);
// print_tiles(tiles, ntiles);
// init empty image
int32_t image_size = (int32_t)sqrt((double)ntiles);
tile_t** image =
(tile_t**)calloc(image_size * image_size, sizeof(tile_t*));
if (!image) {
err(EXIT_FAILURE, "could not allocate memory for image");
}
// For each tile, find NESW neighbors and place in image grid
image[0] = &tiles[0];
tiles[0].y = 0;
tiles[0].x = 0;
STARTOVER:;
for (int32_t i = 0; i < ntiles; i++) {
// only work with tiles already in image
tile_t* t1 = &tiles[i];
if (t1->y < 0) {
continue;
}
for (int32_t j = 0; j < ntiles; j++) {
// skip tiles already in image (incl. self)
tile_t* t2 = &tiles[j];
if (t2->x >= 0) {
continue;
}
// skip tiles at edge of image or for which all neighbors are known
const bool neighbors[4] = {
t1->y > 0 ? image[(t1->y-1) * image_size + t1->x] : false, // N
t1->x < image_size - 1 ? image[t1->y * image_size + t1->x + 1] != NULL : true, // E
t1->y < image_size - 1 ? image[(t1->y+1) * image_size + t1->x] != NULL : true, // S
t1->x > 0 ? image[t1->y * image_size + t1->x - 1] != NULL : false, // W
};
if (neighbors[EDGE_N] && neighbors[EDGE_E] && neighbors[EDGE_S] && neighbors[EDGE_W]) {
continue;
}
if (!neighbors[EDGE_E] && fit_other_on_edge(t1, t2, EDGE_E)) {
t2->y = t1->y;
t2->x = t1->x + 1;
image[t2->y * image_size + t2->x] = t2;
continue;
} else if (!neighbors[EDGE_S] && fit_other_on_edge(t1, t2, EDGE_S)) {
t2->y = t1->y + 1;
t2->x = t1->x;
image[t2->y * image_size + t2->x] = t2;
continue;
} else if (!neighbors[EDGE_N] && fit_other_on_edge(t1, t2, EDGE_N)) {
// if t1 was at northern edge, shift entire image south
if (t1->y == 0) {
shift_image(image, image_size, 1, 0);
}
t2->y = t1->y - 1;
t2->x = t1->x;
image[t2->y * image_size + t2->x] = t2;
continue;
} else if (!neighbors[EDGE_W] && fit_other_on_edge(t1, t2, EDGE_W)) {
// if t1 was at western edge, shift entire image east
if (t1->x == 0) {
shift_image(image, image_size, 0, 1);
}
t2->y = t1->y;
t2->x = t1->x - 1;
image[t2->y * image_size + t2->x] = t2;
continue;
}
}
}
// repeat fitting if image is not yet fully figured out yet
for (int32_t i=0; i < image_size * image_size; i++) {
if (image[i] == NULL) {
goto STARTOVER;
}
}
// print_image_ids(image, image_size);
// print_image(image, image_size);
// generate our final image, plain char array
int32_t final_image_size = (W - 2) * image_size;
char* final_image = (char*) malloc(final_image_size * final_image_size);
if (!final_image) {
err(EXIT_FAILURE, "error allocating memory for image");
}
remove_image_borders(final_image, image, image_size);
int32_t sea_monster_count = 0;
for (int32_t r = 0; r < 3; r++) {
for (int32_t f = 0; f < 2; f++) {
sea_monster_count = count_sea_monster_in_image(
final_image, final_image_size);
if (sea_monster_count > 0) {
goto DETERMINE_WATER_ROUGHNESS;
}
flip(final_image, final_image_size, AX_Y);
}
rotate(final_image, final_image_size);
}
DETERMINE_WATER_ROUGHNESS:;
// count # in monster
int32_t c1 = 0;
for (const char *m = sea_monster.body; *m != '\0'; m++) {
if (*m == '#') {
c1++;
}
}
// count # in image
int32_t c2 = 0;
for (int32_t i = 0; i < final_image_size * final_image_size; i++) {
if (final_image[i] == '#') {
c2++;
}
}
int32_t answer = c2 - (c1 * sea_monster_count);
printf("%d\n", answer);
assert(2009 == answer);
free(final_image);
free(image);
free(tiles);
return 0;
}