-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday20a.c
145 lines (128 loc) · 3.48 KB
/
day20a.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
#include "common.h"
#include "stringview.h"
typedef struct Tile {
int id;
char pixels [10*10];
} Tile_t;
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 flip (char * edge) {
for (int i = 0, j = 9; i < j; ++i, --j) {
char tmp = edge[i];
edge[i] = edge[j];
edge[j] = tmp;
}
}
int main (int argc, char ** argv) {
size_t tiles_count = 0;
struct Tile tiles [144];
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 == 144);
size_t n_corners = 0;
int corners [4];
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 {
flip(e);
if (!strncmp(edge, e, 10)) {
++n_matches;
}
}
}
}
if (!n_matches) {
++n_unique;
}
}
if (n_unique == 2) {
ASSERT(n_corners < ARRAYCOUNT(corners));
corners[n_corners++] = i;
}
}
DISP(n_corners);
size_t result = 1;
for (size_t i = 0; i < n_corners; ++i) {
result *= tiles[corners[i]].id;
}
DISP(result);
}