-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17b.c
262 lines (246 loc) · 9.59 KB
/
day17b.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
#include "common.h"
#include "set.h"
typedef struct Space {
HashSet_t values;
} Space_t;
s64 coordEncode (s16 x, s16 y, s16 z, s16 w) {
union {
s16 coords [4];
s64 value;
} coordunion;
coordunion.coords[0] = x;
coordunion.coords[1] = y;
coordunion.coords[2] = z;
coordunion.coords[3] = w;
return coordunion.value;
}
void coordDecode (s64 v, s16 * x, s16 * y, s16 * z, s16 * w) {
union {
s16 coords [4];
s64 value;
} coordunion;
coordunion.value = v;
*x = coordunion.coords[0];
*y = coordunion.coords[1];
*z = coordunion.coords[2];
*w = coordunion.coords[3];
}
int main (int argc, char ** argv) {
HashSet_t space;
hashset_init(&space, 1024, 0);
s16 xmin = INT16_MAX;
s16 xmax = INT16_MIN;
s16 ymin = INT16_MAX;
s16 ymax = INT16_MIN;
s16 zmin = INT16_MAX;
s16 zmax = INT16_MIN;
s16 wmin = INT16_MAX;
s16 wmax = INT16_MIN;
for (s16 y = 0; y < 8; ++y) {
char buf [BUFSIZ];
char * s = fgets(&(buf[0]), sizeof(buf), stdin);
if (!s) {
break;
}
ASSERT(strlen(s) > 8);
for (s16 x = 0; x < 8; ++x) {
if (s[x] == '#') {
hashset_add(&space, coordEncode(x, y, 0, 0));
xmin = MIN(xmin, x);
xmax = MAX(xmax, x);
ymin = MIN(ymin, y);
ymax = MAX(ymax, y);
zmin = MIN(zmin, 0);
zmax = MAX(zmax, 0);
wmin = MIN(wmin, 0);
wmax = MAX(wmax, 0);
}
}
}
#if 0
printf("Before any cycles:\n");
for (s16 w = wmin; w <= wmax; ++w) {
for (s16 z = zmin; z <= zmax; ++z) {
printf("z=%i, w=%i\n", z, w);
for (s16 y = ymin; y <= ymax; ++y) {
for (s16 x = xmin; x <= xmax; ++x) {
if (hashset_contains(&space, coordEncode(x, y, z, w))) {
printf("#");
} else {
printf(".");
}
}
printf("\n");
}
printf("\n");
}
}
printf("\n");
#endif
for (int cycle = 0; cycle < 6; ++cycle) {
HashSet_t space_next;
hashset_init(&space_next, space.n_bins, 0);
s16 xmin_next = INT16_MAX;
s16 xmax_next = INT16_MIN;
s16 ymin_next = INT16_MAX;
s16 ymax_next = INT16_MIN;
s16 zmin_next = INT16_MAX;
s16 zmax_next = INT16_MIN;
s16 wmin_next = INT16_MAX;
s16 wmax_next = INT16_MIN;
for (s16 w = wmin-1; w <= wmax+1; ++w) {
for (s16 z = zmin-1; z <= zmax+1; ++z) {
for (s16 y = ymin-1; y <= ymax+1; ++y) {
for (s16 x = xmin-1; x <= xmax+1; ++x) {
s16 const dxyzw [80][4] = {
{ -1, -1, -1, -1 },
{ 0, -1, -1, -1 },
{ 1, -1, -1, -1 },
{ -1, 0, -1, -1 },
{ 0, 0, -1, -1 },
{ 1, 0, -1, -1 },
{ -1, 1, -1, -1 },
{ 0, 1, -1, -1 },
{ 1, 1, -1, -1 },
{ -1, -1, 0, -1 },
{ 0, -1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 1, -1, 1, 1 },
{ -1, 0, 1, 1 },
{ 0, 0, 1, 1 },
{ 1, 0, 1, 1 },
{ -1, 1, 1, 1 },
{ 0, 1, 1, 1 },
{ 1, 1, 1, 1 }
};
size_t n_neighbours = 0;
for (size_t i = 0; i < ARRAYCOUNT(dxyzw); ++i) {
s64 in = coordEncode(x+dxyzw[i][0], y+dxyzw[i][1], z+dxyzw[i][2], w+dxyzw[i][3]);
if (hashset_contains(&space, in)) {
n_neighbours += 1;
}
}
if (hashset_contains(&space, coordEncode(x, y, z, w))) {
if (n_neighbours == 2 || n_neighbours == 3) {
hashset_add(&space_next, coordEncode(x, y, z, w));
xmin_next = MIN(xmin_next, x);
xmax_next = MAX(xmax_next, x);
ymin_next = MIN(ymin_next, y);
ymax_next = MAX(ymax_next, y);
zmin_next = MIN(zmin_next, z);
zmax_next = MAX(zmax_next, z);
wmin_next = MIN(wmin_next, w);
wmax_next = MAX(wmax_next, w);
}
} else {
if (n_neighbours == 3) {
hashset_add(&space_next, coordEncode(x, y, z, w));
xmin_next = MIN(xmin_next, x);
xmax_next = MAX(xmax_next, x);
ymin_next = MIN(ymin_next, y);
ymax_next = MAX(ymax_next, y);
zmin_next = MIN(zmin_next, z);
zmax_next = MAX(zmax_next, z);
wmin_next = MIN(wmin_next, w);
wmax_next = MAX(wmax_next, w);
}
}
}
}
}
}
hashset_free(&space);
hashset_move(&space, &space_next);
xmin = xmin_next;
xmax = xmax_next;
ymin = ymin_next;
ymax = ymax_next;
zmin = zmin_next;
zmax = zmax_next;
wmin = wmin_next;
wmax = wmax_next;
#if 0
printf("After %i cycle%s:\n\n", cycle+1, cycle+1==1?"":"s");
for (s16 w = wmin; w <= wmax; ++w) {
for (s16 z = zmin; z <= zmax; ++z) {
printf("z=%i, w=%i\n", z, w);
for (s16 y = ymin; y <= ymax; ++y) {
for (s16 x = xmin; x <= xmax; ++x) {
if (hashset_contains(&space, coordEncode(x, y, z, w))) {
printf("#");
} else {
printf(".");
}
}
printf("\n");
}
printf("\n");
}
}
printf("\n");
#endif
}
DISP(hashset_count(&space));
}