-
Notifications
You must be signed in to change notification settings - Fork 1
/
day22.c
179 lines (145 loc) · 3.04 KB
/
day22.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
#include "common.h"
#define XSZ 10
#define YSZ 10
#define ZSZ 400
struct block { int x1,y1,z1, x2,y2,z2; };
static struct block bricks[1600];
static struct block *grid[ZSZ][YSZ][XSZ];
static size_t nbricks;
static struct block
above(struct block b)
{
b.z1 = b.z2 = b.z2+1;
return b;
}
static struct block
below(struct block b)
{
b.z1 = b.z2 = b.z1-1;
return b;
}
static void
paint(struct block b, struct block *val)
{
int x,y,z;
for (z=b.z1; z<=b.z2; z++)
for (y=b.y1; y<=b.y2; y++)
for (x=b.x1; x<=b.x2; x++)
grid[z][y][x] = val;
}
static size_t
scan(struct block b, struct block **hits, size_t sz)
{
static struct block *buf[5];
struct block *hit;
size_t nhits=0, i;
int x,y,z;
if (!hits) { hits = buf; sz = LEN(buf); }
for (z=b.z1; z<=b.z2; z++)
for (y=b.y1; y<=b.y2; y++)
for (x=b.x1; x<=b.x2; x++) {
if (!(hit = grid[z][y][x]))
continue;
for (i=0; i<nhits && hit != hits[i]; i++)
;
if (i == nhits) {
assert(nhits < sz);
hits[nhits++] = hit;
}
}
return nhits;
}
static void
gravitas(void)
{
size_t i;
int dirty;
for (dirty=1; dirty; )
for (dirty=0, i=0; i<nbricks; i++)
if (bricks[i].z1 && !scan(below(bricks[i]), NULL, 0)) {
paint(below(bricks[i]), &bricks[i]);
bricks[i].z1--;
bricks[i].z2--;
paint(above(bricks[i]), NULL);
dirty = 1;
}
}
static int
jenga(void)
{
static struct block *hits[5];
size_t nabove, i,j;
int count=0;
for (i=0; i<nbricks; i++) {
nabove = scan(above(bricks[i]), hits, (int)LEN(hits));
for (j=0; j<nabove; j++)
if (scan(below(*hits[j]), NULL, 0) < 2)
break;
count += j == nabove;
}
return count;
}
static void
disintegrate(struct block *b, uint8_t *gone)
{
struct block *up[5], *down[5];
size_t nup, ndown, i,j;
if (gone[b-bricks])
return;
gone[b-bricks] = 1;
nup = scan(above(*b), up, LEN(up));
for (i=0; i<nup; i++) {
ndown = scan(below(*up[i]), down, LEN(down));
for (j=0; j<ndown; j++)
if (!gone[down[j]-bricks])
break;
if (j == ndown)
disintegrate(up[i], gone);
}
}
static int
godzilla(void)
{
static uint8_t gone[LEN(bricks)];
size_t i,j;
int ngone=0;
for (i=0; i<nbricks; i++) {
memset(gone, 0, sizeof(gone));
disintegrate(&bricks[i], gone);
for (j=0; j<nbricks; j++)
ngone += gone[j];
}
return ngone - nbricks; /* don't count fist bricks */
}
int
main(int argc, char **argv)
{
struct block *b;
int p1,p2, ntok;
if (argc > 1)
DISCARD(freopen(argv[1], "r", stdin));
for (; ; nbricks++) {
assert(nbricks < LEN(bricks));
b = &bricks[nbricks];
ntok = scanf("%d,%d,%d~%d,%d,%d",
&b->x1, &b->y1, &b->z1,
&b->x2, &b->y2, &b->z2);
if (ntok < 6)
break;
assert(b->x1 <= b->x2);
assert(b->y1 <= b->y2);
assert(b->z1 <= b->z2);
assert(b->x1 >= 0); assert(b->x1 < XSZ);
assert(b->x2 >= 0); assert(b->x2 < XSZ);
assert(b->y1 >= 0); assert(b->y1 < YSZ);
assert(b->y2 >= 0); assert(b->y2 < YSZ);
assert(b->z1 >= 0); assert(b->z1 < ZSZ);
assert(b->z2 >= 0); assert(b->z2 < ZSZ);
paint(*b, b);
}
gravitas();
p1 = jenga();
p2 = godzilla();
printf("22: %d %d\n", p1, p2);
return 0;
}