-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday20b.c
427 lines (391 loc) · 12.3 KB
/
day20b.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
#include "common.h"
#include "stringview.h"
#include "set.h"
typedef struct Tile {
int id;
char pixels [10*10];
} Tile_t;
void printTile (struct Tile * tile) {
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
putchar(tile->pixels[i*10+j]);
}
printf("\n");
}
printf("\n");
}
enum Side {
SIDE_TOP = 0,
SIDE_RIGHT = 1,
SIDE_BOTTOM = 2,
SIDE_LEFT = 3
};
int edgeToInt (char * edge) {
int v = 0;
for (int i = 0; i < 10; ++i) {
v = (v << 1) | (edge[i] == '#');
}
return v;
}
void getEdge (struct Tile * tile, enum Side side, char * out) {
int ind = 0;
int dind = 0;
switch (side) {
case SIDE_TOP: {
ind = 0;
dind = 1;
} break;
case SIDE_RIGHT: {
ind = 9;
dind = 10;
} break;
case SIDE_BOTTOM: {
ind = 99;
dind = -1;
} break;
case SIDE_LEFT: {
ind = 90;
dind = -10;
} break;
}
for (int i = 0; i < 10; ++i) {
*(out++) = tile->pixels[ind];
ind += dind;
}
}
void flipEdge (char * edge) {
for (int i = 0, j = 9; i < j; ++i, --j) {
char tmp = edge[i];
edge[i] = edge[j];
edge[j] = tmp;
}
}
void flipTileHorizontal (struct Tile * tile) {
for (int i = 0; i < 10; ++i) {
for (int j = 0, k = 9; j < k; ++j, --k) {
char tmp = tile->pixels[i*10+j];
tile->pixels[i*10+j] = tile->pixels[i*10+k];
tile->pixels[i*10+k] = tmp;
}
}
}
void flipTileVertical (struct Tile * tile) {
for (int i = 0; i < 10; ++i) {
for (int j = 0, k = 9; j < k; ++j, --k) {
char tmp = tile->pixels[i+j*10];
tile->pixels[i+j*10] = tile->pixels[i+k*10];
tile->pixels[i+k*10] = tmp;
}
}
}
void rotateTileRight (struct Tile * tile) {
char tmp [10*10];
memcpy(tmp, &(tile->pixels), 10*10);
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
tile->pixels[i*10+j] = tmp[(9-j)*10+i];
}
}
}
void rotateTileLeft (struct Tile * tile) {
char tmp [10*10];
memcpy(tmp, &(tile->pixels), 10*10);
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
tile->pixels[i*10+j] = tmp[j*10+(9-i)];
}
}
}
void rotateTile180 (struct Tile * tile) {
char tmp [10*10];
memcpy(tmp, &(tile->pixels), 10*10);
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
tile->pixels[i*10+j] = tmp[(9-i)*10+(9-j)];
}
}
}
int main (int argc, char ** argv) {
size_t tiles_count = 0;
struct Tile tiles [12*12];
do {
char buf [BUFSIZ];
char * s = fgets(&(buf[0]), sizeof(buf), stdin);
if (!s) {
break;
}
StringView_t line = sv_view_c_string(s);
sv_eat_spaces(&line);
if (sv_is_empty(&line)) {
continue;
}
StringView_t tile_str = sv_eat_until_space(&line);
if (sv_equals(&tile_str, "Tile")) {
sv_eat_spaces(&line);
long id = strtol(line.start, NULL, 10);
ASSERT(tiles_count < ARRAYCOUNT(tiles));
long index = tiles_count++;
struct Tile * tile = &(tiles[index]);
tile->id = id;
for (int i = 0; i < 10; ++i) {
char * s = fgets(&(buf[0]), sizeof(buf), stdin);
ASSERT(s);
ASSERT(strlen(s) == 10+1);
for (int j = 0; j < 10; ++j) {
tile->pixels[i*10+j] = *(s++);
}
}
}
} while (1);
ASSERT(tiles_count == 12*12);
Set_t corners;
set_init(&corners, 4);
Set_t unplaced;
set_init(&unplaced, 144);
for (size_t i = 0; i < tiles_count; ++i) {
Tile_t * tile = &(tiles[i]);
size_t n_unique = 0;
for (enum Side side = SIDE_TOP; side <= SIDE_LEFT; ++side) {
char edge [10];
getEdge(tile, side, &(edge[0]));
size_t n_matches = 0;
for (size_t j = 0; j < tiles_count; ++j) {
if (i == j) {
continue;
}
Tile_t * other = &(tiles[j]);
for (enum Side s = SIDE_TOP; s <= SIDE_LEFT; ++s) {
char e [10];
getEdge(other, s, &(e[0]));
if (!strncmp(edge, e, 10)) {
++n_matches;
} else {
flipEdge(e);
if (!strncmp(edge, e, 10)) {
++n_matches;
}
}
}
}
ASSERT(n_matches == 0 || n_matches == 1);
if (n_matches == 0) {
n_unique += 1;
}
}
set_add(&unplaced, i);
if (n_unique == 2) {
set_add(&corners, i);
}
}
ASSERT(corners.count == 4);
int grid [12*12];
for (int i = 0; i < 12*12; ++i) {
grid[i] = -1;
}
// NB: The third corner is used in order to match the orientation used by the puzzle setter
grid[0] = corners.values[2];
set_remove(&unplaced, grid[0]);
{
Tile_t * tile = &(tiles[grid[0]]);
size_t counts [4];
for (enum Side side = SIDE_TOP; side <= SIDE_LEFT; ++side) {
char edge [10];
getEdge(tile, side, &(edge[0]));
size_t n_matches = 0;
for (size_t j = 0; j < tiles_count; ++j) {
Tile_t * other = &(tiles[j]);
if (other == tile) {
continue;
}
for (enum Side s = SIDE_TOP; s <= SIDE_LEFT; ++s) {
char e [10];
getEdge(other, s, &(e[0]));
if (!strncmp(edge, e, 10)) {
++n_matches;
} else {
flipEdge(e);
if (!strncmp(edge, e, 10)) {
++n_matches;
}
}
}
}
counts[side] = n_matches;
}
if (counts[0] == 0 && counts[3] == 0) {
// Corner is already orientated correctly
} else if (counts[0] == 0 && counts[1] == 0) {
rotateTileLeft(tile);
} else if (counts[1] == 0 && counts[2] == 0) {
rotateTile180(tile);
} else if (counts[2] == 0 && counts[3] == 0) {
rotateTileRight(tile);
} else {
ASSERT(0);
}
// NB: Corner tile is flipped and rotated in order to match the orientation used by the puzzle setter
flipTileVertical(tile);
rotateTileRight(tile);
}
for (int i = 1; i < 12; ++i) {
Tile_t * tile = &(tiles[grid[i-1]]);
enum Side side = SIDE_RIGHT;
char edge [10];
getEdge(tile, side, &(edge[0]));
int other_index = -1;
enum Side matching_side;
_Bool flipped;
for (size_t j = 0; j < unplaced.count; ++j) {
Tile_t * other = &(tiles[unplaced.values[j]]);
for (enum Side s = SIDE_TOP; s <= SIDE_LEFT; ++s) {
char e [10];
getEdge(other, s, &(e[0]));
if (!strncmp(edge, e, 10)) {
other_index = unplaced.values[j];
matching_side = s;
flipped = 0;
break;
} else {
flipEdge(e);
if (!strncmp(edge, e, 10)) {
other_index = unplaced.values[j];
matching_side = s;
flipped = 1;
break;
}
}
}
if (other_index != -1) {
break;
}
}
ASSERT(other_index != -1);
Tile_t * other = &(tiles[other_index]);
if (matching_side == SIDE_LEFT) {
// Tile is already oriented correctly
} else if (matching_side == SIDE_TOP) {
rotateTileLeft(other);
} else if (matching_side == SIDE_RIGHT) {
rotateTile180(other);
} else if (matching_side == SIDE_BOTTOM) {
rotateTileRight(other);
} else {
ASSERT(0);
}
if (!flipped) {
flipTileVertical(other);
}
grid[i] = other_index;
set_remove(&unplaced, other_index);
}
for (int i = 12; i < 12*12; ++i) {
Tile_t * tile = &(tiles[grid[i-12]]);
enum Side side = SIDE_BOTTOM;
char edge [10];
getEdge(tile, side, &(edge[0]));
int other_index = -1;
enum Side matching_side;
_Bool flipped;
for (size_t j = 0; j < unplaced.count; ++j) {
Tile_t * other = &(tiles[unplaced.values[j]]);
for (enum Side s = SIDE_TOP; s <= SIDE_LEFT; ++s) {
char e [10];
getEdge(other, s, &(e[0]));
if (!strncmp(edge, e, 10)) {
other_index = unplaced.values[j];
matching_side = s;
flipped = 0;
break;
} else {
flipEdge(e);
if (!strncmp(edge, e, 10)) {
other_index = unplaced.values[j];
matching_side = s;
flipped = 1;
break;
}
}
}
if (other_index != -1) {
break;
}
}
ASSERT(other_index != -1);
Tile_t * other = &(tiles[other_index]);
if (matching_side == SIDE_LEFT) {
rotateTileRight(other);
} else if (matching_side == SIDE_TOP) {
// Tile is already oriented correctly
} else if (matching_side == SIDE_RIGHT) {
rotateTileLeft(other);
} else if (matching_side == SIDE_BOTTOM) {
rotateTile180(other);
} else {
ASSERT(0);
}
if (!flipped) {
flipTileHorizontal(other);
}
grid[i] = other_index;
set_remove(&unplaced, other_index);
}
ASSERT(unplaced.count == 0);
char image [8*8*12*12];
for (int i = 0; i < 12; ++i) {
for (int j = 0; j < 12; ++j) {
Tile_t * tile = &(tiles[grid[i*12+j]]);
for (int k = 0; k < 8; ++k) {
for (int l = 0; l < 8; ++l) {
image[(i*8+k)*8*12+j*8+l] = tile->pixels[(k+1)*10+(l+1)];
}
}
}
}
size_t n_monsters = 0;
for (int i = 0; i < 8*12-2; ++i) {
for (int j = 0; j < 8*12-19; ++j) {
int I = i*8*12+j;
if (image[I+0*8*12+18] == '#' &&
image[I+1*8*12+ 0] == '#' &&
image[I+1*8*12+ 5] == '#' &&
image[I+1*8*12+ 6] == '#' &&
image[I+1*8*12+11] == '#' &&
image[I+1*8*12+12] == '#' &&
image[I+1*8*12+17] == '#' &&
image[I+1*8*12+18] == '#' &&
image[I+1*8*12+19] == '#' &&
image[I+2*8*12+ 1] == '#' &&
image[I+2*8*12+ 4] == '#' &&
image[I+2*8*12+ 7] == '#' &&
image[I+2*8*12+10] == '#' &&
image[I+2*8*12+13] == '#' &&
image[I+2*8*12+16] == '#')
{
image[I+0*8*12+18] = 'O';
image[I+1*8*12+ 0] = 'O';
image[I+1*8*12+ 5] = 'O';
image[I+1*8*12+ 6] = 'O';
image[I+1*8*12+11] = 'O';
image[I+1*8*12+12] = 'O';
image[I+1*8*12+17] = 'O';
image[I+1*8*12+18] = 'O';
image[I+1*8*12+19] = 'O';
image[I+2*8*12+ 1] = 'O';
image[I+2*8*12+ 4] = 'O';
image[I+2*8*12+ 7] = 'O';
image[I+2*8*12+10] = 'O';
image[I+2*8*12+13] = 'O';
image[I+2*8*12+16] = 'O';
n_monsters += 1;
}
}
}
//DISP(n_monsters);
size_t answer = 0;
for (size_t i = 0; i < ARRAYCOUNT(image); ++i) {
if (image[i] == '#') {
answer += 1;
}
}
DISP(answer);
}