-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday24b.c
159 lines (144 loc) · 3.81 KB
/
day24b.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
#include "common.h"
#include "set.h"
s64 pack (s32 q, s32 r) {
union {
s32 coords [2];
s64 packedvalue;
} coordunion;
coordunion.coords[0] = q;
coordunion.coords[1] = r;
return coordunion.packedvalue;
}
void unpack (s64 v, s32 * q, s32 * r) {
union {
s32 coords [2];
s64 packedvalue;
} coordunion;
coordunion.packedvalue = v;
*q = coordunion.coords[0];
*r = coordunion.coords[1];
}
enum Direction {
DIR_E,
DIR_NE,
DIR_NW,
DIR_W,
DIR_SW,
DIR_SE
};
void offset (s32 * q, s32 * r, enum Direction direction, s32 distance) {
s32 dq = 0;
s32 dr = 0;
switch (direction) {
case DIR_E: {
dq = 1;
} break;
case DIR_NE: {
dr = 1;
} break;
case DIR_NW: {
dq = -1;
dr = 1;
} break;
case DIR_W: {
dq = -1;
} break;
case DIR_SW: {
dr = -1;
} break;
case DIR_SE: {
dq = 1;
dr = -1;
} break;
}
*q += dq*distance;
*r += dr*distance;
}
int main (int argc, char ** argv) {
Set_t tiles;
set_init(&tiles, 512);
do {
char buf [BUFSIZ];
char * s = fgets(&(buf[0]), sizeof(buf), stdin);
if (!s) {
break;
}
s32 q = 0;
s32 r = 0;
while (*s != '\n') {
enum Direction dir = DIR_E;
if (*s == 'e') {
dir = DIR_E;
++s;
} else if (*s == 'w') {
dir = DIR_W;
++s;
} else if (*s == 'n') {
++s;
if (*s == 'e') {
dir = DIR_NE;
} else if (*s == 'w') {
dir = DIR_NW;
}
++s;
} else if (*s == 's') {
++s;
if (*s == 'e') {
dir = DIR_SE;
} else if (*s == 'w') {
dir = DIR_SW;
}
++s;
} else {
ASSERT(0);
}
offset(&q, &r, dir, 1);
}
s64 coordval = pack(q, r);
if (set_contains(&tiles, coordval)) {
set_remove(&tiles, coordval);
} else {
set_add(&tiles, coordval);
}
} while (1);
for (int day = 0; day < 100; ++day) {
Set_t tiles_new;
set_copy(&tiles_new, &tiles);
s32 qmin = INT32_MAX, qmax = INT32_MIN;
s32 rmin = INT32_MAX, rmax = INT32_MIN;
for (intptr_t const * it = set_cbegin(&tiles); it != set_cend(&tiles); ++it) {
s32 q, r;
unpack(*it, &q, &r);
qmin = MIN(qmin, q);
qmax = MAX(qmax, q);
rmin = MIN(rmin, r);
rmax = MAX(rmax, r);
}
for (s32 q = qmin-1; q < qmax+2; ++q) {
for (s32 r = rmin-1; r < rmax+2; ++r) {
int n_neighbours = 0;
for (enum Direction dir = DIR_E; dir <= DIR_SE; ++dir) {
s32 qn = q;
s32 rn = r;
offset(&qn, &rn, dir, 1);
if (set_contains(&tiles, pack(qn, rn))) {
++n_neighbours;
}
}
s64 coordval = pack(q, r);
if (set_contains(&tiles, coordval)) {
if (n_neighbours == 0 || n_neighbours > 2) {
set_remove(&tiles_new, coordval);
}
} else {
if (n_neighbours == 2) {
set_add(&tiles_new, coordval);
}
}
}
}
set_free(&tiles);
set_move(&tiles, &tiles_new);
}
DISP(tiles.count);
}