-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17a.c
188 lines (172 loc) · 5.82 KB
/
day17a.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
#include "common.h"
#include "set.h"
typedef struct Space {
HashSet_t values;
} Space_t;
s64 coordEncode (s16 x, s16 y, s16 z) {
union {
s16 coords [3];
s64 value;
} coordunion;
coordunion.coords[0] = x;
coordunion.coords[1] = y;
coordunion.coords[2] = z;
return coordunion.value;
}
void coordDecode (s64 v, s16 * x, s16 * y, s16 * z) {
union {
s16 coords [3];
s64 value;
} coordunion;
coordunion.value = v;
*x = coordunion.coords[0];
*y = coordunion.coords[1];
*z = coordunion.coords[2];
}
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;
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));
xmin = MIN(xmin, x);
xmax = MAX(xmax, x);
ymin = MIN(ymin, y);
ymax = MAX(ymax, y);
zmin = MIN(zmin, 0);
zmax = MAX(zmax, 0);
}
}
}
#if 0
printf("Before any cycles:\n");
for (s16 z = zmin; z <= zmax; ++z) {
printf("z=%i\n", z);
for (s16 y = ymin; y <= ymax; ++y) {
for (s16 x = xmin; x <= xmax; ++x) {
if (hashset_contains(&space, coordEncode(x, y, z))) {
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;
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 dxyz [26][3] = {
{ -1, -1, -1 },
{ 0, -1, -1 },
{ 1, -1, -1 },
{ -1, 0, -1 },
{ 0, 0, -1 },
{ 1, 0, -1 },
{ -1, 1, -1 },
{ 0, 1, -1 },
{ 1, 1, -1 },
{ -1, -1, 0 },
{ 0, -1, 0 },
{ 1, -1, 0 },
{ -1, 0, 0 },
//{ 0, 0, 0 }, SELF
{ 1, 0, 0 },
{ -1, 1, 0 },
{ 0, 1, 0 },
{ 1, 1, 0 },
{ -1, -1, 1 },
{ 0, -1, 1 },
{ 1, -1, 1 },
{ -1, 0, 1 },
{ 0, 0, 1 },
{ 1, 0, 1 },
{ -1, 1, 1 },
{ 0, 1, 1 },
{ 1, 1, 1 }
};
size_t n_neighbours = 0;
for (size_t i = 0; i < ARRAYCOUNT(dxyz); ++i) {
s64 in = coordEncode(x+dxyz[i][0], y+dxyz[i][1], z+dxyz[i][2]);
if (hashset_contains(&space, in)) {
n_neighbours += 1;
}
}
if (hashset_contains(&space, coordEncode(x, y, z))) {
if (n_neighbours == 2 || n_neighbours == 3) {
hashset_add(&space_next, coordEncode(x, y, z));
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);
}
} else {
if (n_neighbours == 3) {
hashset_add(&space_next, coordEncode(x, y, z));
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);
}
}
}
}
}
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;
#if 0
printf("After %i cycle%s:\n\n", cycle+1, cycle+1==1?"":"s");
for (s16 z = zmin; z <= zmax; ++z) {
printf("z=%i\n", z);
for (s16 y = ymin; y <= ymax; ++y) {
for (s16 x = xmin; x <= xmax; ++x) {
if (hashset_contains(&space, coordEncode(x, y, z))) {
printf("#");
} else {
printf(".");
}
}
printf("\n");
}
printf("\n");
}
printf("\n");
#endif
}
DISP(hashset_count(&space));
}