-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24.c
294 lines (277 loc) · 7.84 KB
/
24.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TILES 592
// A guess.
#define MAX_TILES 10000
#define ITERATION 100
/*
* - 0 1 2 3 4 5
* - 0 1 2 3 4 5
* 0 1 2 3 4 5
*/
typedef struct Coord{
int x;
int y;
// 0 -> not registered, 1 -> flipped, 2 -> unflipped
int flipState;
} Coord;
Coord *getCoords(FILE *in);
Coord parseLine(char *line);
Coord *iterate(Coord *current);
int countFlipped(Coord *coords);
int areAdjacent(Coord a, Coord b);
int minX(Coord *coords);
int minY(Coord *coords);
int maxX(Coord *coords);
int maxY(Coord *coords);
int isFlipped(Coord *coords, Coord coord);
int getAdjacent(Coord *coords, Coord coord);
void printCoords(Coord *coords, Coord highlight);
int main(int argc, char *argv[])
{
FILE *in = fopen("in24", "r");
Coord *coords = getCoords(in);
printf("Flipped to black: %d\n", countFlipped(coords));
for (int i = 0; i < 100; i++)
coords = iterate(coords);
printf("Finally flipped to blacK: %d\n", countFlipped(coords));
free(coords);
fclose(in);
return 0;
}
void printCoords(Coord *coords, Coord highlight)
{
int miX = minX(coords);
int maX = maxX(coords);
int miY = minY(coords);
int maY = maxY(coords);
int *array = calloc((maX-miX+1)*(maY-miY+1), sizeof(*array));
for (int i = 0; coords[i].flipState; i++)
if (coords[i].flipState == 1)
{
array[coords[i].x - miX + (coords[i].y - miY) * (maX-miX+1)] =
coords[i].x == highlight.x && coords[i].y == highlight.y
? 2
: 1;
}
if (highlight.x >= miX && highlight.y >= miY &&
highlight.x <= maX && highlight.y <= maY &&
!array[highlight.x - miX + (highlight.y - miY) * (maX-miX+1)])
array[highlight.x - miX + (highlight.y - miY) * (maX-miX+1)] = -1;
for (int y = maY-miY; y >= 0; y--)
{
printf("%2d ", y);
for (int i = 0; i < y; i++)
printf(" ");
for (int x = 0; x <= maX-miX; x++)
{
switch(array[x + y * (maX-miX+1)])
{
case 1:
printf("%d ", x);
break;
case 2:
printf("█ ");
break;
case -1:
printf("_ ");
break;
default:
printf(". ");
break;
}
}
printf("\n");
}
free(array);
}
Coord *iterate(Coord *current)
{
//Coord highlight = {0};
//highlight.x = 1000;
//highlight.y = 1000;
//printCoords(current, highlight);
//printf("\n=================\n");
Coord *clone = malloc(MAX_TILES * sizeof(*clone));
int cloneIndex = 0;
memcpy(clone, current, MAX_TILES * sizeof(*clone));
Coord coord = {0};
int miX = minX(current);
int maX = maxX(current);
int miY = minY(current);
int maY = maxY(current);
for (coord.x = miX-1; coord.x <= maX+1; coord.x++)
for (coord.y = miY-1; coord.y <= maY+1; coord.y++)
{
int flipped = isFlipped(current, coord);
// We only register new flipped ones
coord.flipState = 1;
int adjacent = getAdjacent(current, coord);
if (flipped && (adjacent == 1 || adjacent == 2))
{
// Stays black
//printf("(%d,%d) has %d adjacents - stays flipped\n", coord.x, coord.y, adjacent);
//printCoords(current, coord);
clone[cloneIndex++] = coord;
}
else if (!flipped && adjacent == 2)
{
// Flips
//printf("(%d,%d) has %d adjacents - flips\n", coord.x, coord.y, adjacent);
//printCoords(current, coord);
clone[cloneIndex++] = coord;
}
}
free(current);
for (; cloneIndex < MAX_TILES; cloneIndex++)
clone[cloneIndex].flipState = 0;
//printf("Currently flipped: %d (cloneIndex: %d)\n", countFlipped(clone), cloneIndex);
return clone;
}
int getAdjacent(Coord *coords, Coord coord)
{
int res = 0;
for (int i = 0; coords[i].flipState; i++)
if (areAdjacent(coords[i], coord))
{
if (coords[i].flipState == 1)
// TODO: Something's wrong
//printf("(%d,%d) adjacent to (%d,%d)\n", coords[i].x, coords[i].y, coord.x, coord.y);
res += (coords[i].flipState == 1);
}
return res;
}
int isFlipped(Coord *coords, Coord coord)
{
for (int i = 0; coords[i].flipState; i++)
if (coords[i].x == coord.x && coords[i].y == coord.y)
return coords[i].flipState == 1;
return 0;
}
int minX(Coord *coords)
{
int min = coords[0].x;
for (int i = 1; coords[i].flipState; i++)
if (coords[i].x < min)
min = coords[i].x;
return min;
}
int minY(Coord *coords)
{
int min = coords[0].y;
for (int i = 1; coords[i].flipState; i++)
if (coords[i].y < min)
min = coords[i].y;
return min;
}
int maxX(Coord *coords)
{
int max = coords[0].x;
for (int i = 1; coords[i].flipState; i++)
if (coords[i].x > max)
max = coords[i].x;
return max;
}
int maxY(Coord *coords)
{
int max = coords[0].y;
for (int i = 1; coords[i].flipState; i++)
if (coords[i].y > max)
max = coords[i].y;
return max;
}
int areAdjacent(Coord a, Coord b)
{
return (a.y == b.y && (a.x == b.x-1 || a.x == b.x+1)) ||
(a.y == b.y+1 && (a.x == b.x-1 || a.x == b.x)) ||
(a.y == b.y-1 && (a.x == b.x || a.x == b.x+1));
}
int countFlipped(Coord *coords)
{
int res = 0;
for (int i = 0; i < MAX_TILES; i++)
{
res += coords[i].flipState == 1;
//if (coords[i].flipState == 1)
//printf("(%d,%d) ", coords[i].x, coords[i].y);
}
return res;
}
Coord *getCoords(FILE *in)
{
Coord *res = calloc(MAX_TILES, sizeof(*res));
size_t n = 0;
char *line = NULL;
while (getline(&line, &n, in) >= 0 && *line != '\n')
{
*strchr(line, '\n') = '\0';
Coord parsed = parseLine(line);
for (int i = 0; i < TILES; i++)
{
if (res[i].flipState && res[i].x == parsed.x && res[i].y == parsed.y)
{
res[i].flipState = res[i].flipState == 1 ? 2 : 1;
break;
}
if (!res[i].flipState)
{
res[i] = parsed;
res[i].flipState = 1;
break;
}
}
}
free(line);
return res;
}
Coord parseLine(char *line)
{
Coord res = {0};
for (int i = 0; i < strlen(line); i++)
{
switch(line[i])
{
case 'e':
res.x++;
break;
case 'w':
res.x--;
break;
case 'n':
res.y++;
i++;
switch(line[i])
{
case 'e':
break;
case 'w':
res.x--;
break;
default:
fprintf(stderr, "Unexpected follow up to n: %c\n", line[i]);
exit(1);
}
break;
case 's':
res.y--;
i++;
switch(line[i])
{
case 'e':
res.x++;
break;
case 'w':
break;
default:
fprintf(stderr, "Unexpected follow up to s: %c\n", line[i]);
exit(1);
}
break;
default:
fprintf(stderr, "Unexpected: %c (%s)\n", line[i], line);
exit(1);
}
}
return res;
}