-
Notifications
You must be signed in to change notification settings - Fork 2
/
18.c
242 lines (207 loc) · 5.57 KB
/
18.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
#include "adventofcode.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define PUZZLE_NAME "Day 18: Settlers of The North Pole"
static size_t parse(char *grid) {
char *g = grid;
char buf[1024];
size_t len = 0;
while (fgets(buf, 1024, stdin)) {
len = strlen(buf) - 1;
memcpy(g, buf, len);
g += len;
}
return len;
}
struct Count {
int trees;
int lumberyards;
int open;
};
static struct Count visit_adjacent(const char grid[static 50*50], const size_t width,
const unsigned int r1,
const unsigned int c1) {
struct Count cnt = {0, 0, 0};
for (int rd = -1; rd <= 1; rd++) {
for (int cd = -1; cd <= 1; cd++) {
if (rd == 0 && cd == 0) {
continue;
}
unsigned r2 = (unsigned)((int)r1 + rd);
unsigned c2 = (unsigned)((int)c1 + cd);
if (r2 >= width || c2 >= width) {
continue;
}
switch (grid[r2 * width + c2]) {
case '#':
cnt.lumberyards++;
break;
case '|':
cnt.trees++;
break;
case '.':
cnt.open++;
break;
};
}
}
return cnt;
}
static void step(const size_t width, char grid[static 50*50],
char next[static 50*50]) {
for (unsigned r = 0; r < width; r++) {
for (unsigned c = 0; c < width; c++) {
const struct Count cnt = visit_adjacent(grid, width, r, c);
const size_t pos = r * width + c;
switch (grid[pos]) {
case '.':
if (cnt.trees >= 3) {
next[pos] = '|';
} else {
next[pos] = '.';
}
break;
case '|':
if (cnt.lumberyards >= 3) {
next[pos] = '#';
} else {
next[pos] = '|';
}
break;
case '#':
if (cnt.trees >= 1 && cnt.lumberyards >= 1) {
next[pos] = '#';
} else {
next[pos] = '.';
}
break;
}
}
}
// swap pointers
memcpy(grid, next, width * width * sizeof(char));
}
struct Cycle {
/* index of first repetition */
unsigned int mu;
/* shortest cycle length */
unsigned int lam;
};
/*
Floyd's tortoise and hare cycle-finding algorithm
See https://en.wikipedia.org/wiki/Cycle_detection
*/
static struct Cycle floyd(const size_t width, const char grid[static 50*50]) {
const size_t n = width * width * sizeof(char);
char *tortoise = malloc(n);
char *hare = malloc(n);
assert(tortoise != NULL);
assert(hare != NULL);
char *next = malloc(n);
assert(next != NULL);
/*
Main phase of algorithm: finding a repetition x_i = x_2i.
The hare moves twice as quickly as the tortoise and
the distance between them increases by 1 at each step.
Eventually they will both be inside the cycle and then,
at some point, the distance between them will be
divisible by the period λ.
*/
memcpy(tortoise, grid, n);
memcpy(hare, grid, n);
step(width, tortoise, next);
step(width, hare, next);
step(width, hare, next);
while (memcmp(tortoise, hare, n) != 0) {
step(width, tortoise, next);
step(width, hare, next);
step(width, hare, next);
}
/*
At this point the tortoise position, ν, which is also equal
to the distance between hare and tortoise, is divisible by
the period λ. So hare moving in cycle one step at a time,
and tortoise (reset to x0) moving towards the cycle, will
intersect at the beginning of the cycle. Because the
distance between them is constant at 2ν, a multiple of λ,
they will agree as soon as the tortoise reaches index μ.
Find the position μ of first repetition.
*/
unsigned mu = 0;
memcpy(tortoise, grid, n);
while (memcmp(tortoise, hare, n) != 0) {
step(width, tortoise, next);
step(width, hare, next);
mu++;
}
/*
Find the length of the shortest cycle starting from x_μ
The hare moves one step at a time while tortoise is still.
lam is incremented until λ is found.
*/
unsigned lam = 1;
step(width, hare, next);
while (memcmp(tortoise, hare, n) != 0) {
step(width, hare, next);
lam++;
}
free(tortoise);
free(hare);
free(next);
return (struct Cycle){mu, lam};
}
static unsigned grid_value(const size_t width, const char grid[static 50*50]) {
unsigned trees = 0;
unsigned lumberyards = 0;
const size_t sz = width * width;
for (size_t i = 0; i < sz; i++) {
switch (grid[i]) {
case '|':
trees += 1;
break;
case '#':
lumberyards += 1;
break;
}
}
return trees * lumberyards;
}
static unsigned int nsteps(const size_t width, const char grid[static 50*50],
unsigned nsteps) {
const size_t n = width * width * sizeof(char);
char *cur = malloc(n);
assert(cur != NULL);
memcpy(cur, grid, n);
char *next = malloc(n);
assert(next != NULL);
while (nsteps > 0) {
step(width, cur, next);
nsteps -= 1;
}
unsigned int value = grid_value(width, cur);
free(cur);
free(next);
return value;
}
int main(void) {
clock_t t = clock_time();
size_t grid_size = 50 * 50;
char *grid = malloc(grid_size * sizeof(char));
assert(grid != NULL);
const size_t width = parse(grid);
assert(width <= 50);
const unsigned int pt1 = nsteps(width, grid, 10);
/* Detect cycle for part 2 */
const struct Cycle c = floyd(width, grid);
const unsigned int pt2 =
nsteps(width, grid, c.mu + ((1000000000u - c.mu) % c.lam));
free(grid);
printf("--- %s ---\n", PUZZLE_NAME);
printf("Part 1: %d\n", pt1);
printf("Part 2: %d\n", pt2);
printf("Time: %.2fms\n", clock_time_since(t));
return EXIT_SUCCESS;
}