-
Notifications
You must be signed in to change notification settings - Fork 1
/
day14.c3
262 lines (242 loc) · 5.47 KB
/
day14.c3
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
/*
* Advent of Code 2023 day 14
*/
import std::io;
import std::time;
import std::math;
import std::collections::ringbuffer;
import std::hash::fnv64a;
fn void main()
{
io::printn("Advent of code, day 14.");
@pool()
{
// Simple benchmarking with Clock, "mark" returns the last duration and resets the clock
Clock c = clock::now();
io::printfn("* Sum part 1: %d - completed in %s", part1(), c.mark());
io::printfn("* Sum part 2: %d - completed in %s", part2(), c.mark());
};
}
fn long part1()
{
File f = file::open("day14.txt", "rb")!!;
defer (void)f.close();
// For the rocks, let's try a very simple algorithm.
int[200] rocks;
int width = 0;
int height = 0;
// First let's just grab the height and width.
while (try line = io::treadline(&f))
{
height++;
width = line.len;
}
rocks[:width] = height + 1;
// Go back in the file.
f.seek(0, SET)!!;
int sum = 0;
int height_checked = height;
while (try line = io::treadline(&f))
{
// For each line, iterate over every column.
for (int i = 0; i < width; i++)
{
switch (line[i])
{
case 'O':
// Add the value of the rock - 1 in the column
// Reduce the next rock value by one, since it is now occupied.
sum += rocks[i] - 1;
rocks[i]--;
case '#':
// Finding a fixed rock means setting the rock value to the
// height making the next rock having the value height_checked - 1
rocks[i] = height_checked;
}
}
// Go down one line.
height_checked--;
}
return sum;
}
// Let's build a map with operator overloading.
struct Map
{
char[200][] data;
int width;
}
// This is what we need to implement to have indexing and foreach on the type
fn isz Map.len(self) @inline @operator(len) => self.width;
fn char[] Map.get(self, isz i) @inline @operator([]) => self.data[i][:self.width];
// Brute force tilt north.
fn void tilt_north(Map map)
{
foreach (int h, line : map)
{
foreach (w, c : line)
{
if (c != 'O') continue;
isz new_h = h;
while (new_h > 0 && map[new_h - 1][w] == '.') new_h--;
if (new_h != h)
{
line[w] = '.';
map[new_h][w] = 'O';
}
}
}
}
// Brute force tilt south
fn void tilt_south(Map map)
{
foreach_r (int h, line : map)
{
foreach (w, c : line)
{
if (c != 'O') continue;
isz new_h = h;
while (new_h < map.len() - 1 && map[new_h + 1][w] == '.') new_h++;
if (new_h != h)
{
line[w] = '.';
map[new_h][w] = 'O';
}
}
}
}
// Brute force tilt west
fn void tilt_west(Map map)
{
foreach (line : map)
{
foreach (int w, c : line)
{
if (!w) continue;
if (c != 'O') continue;
isz new_w = w;
while (new_w > 0 && line[new_w - 1] == '.') new_w--;
if (new_w != w)
{
line[w] = '.';
line[new_w] = 'O';
}
}
}
}
// Brute force tilt east
fn void tilt_east(Map map)
{
foreach (int h, line : map)
{
foreach_r (int w, c : line)
{
if (w == map.width - 1) continue;
if (c != 'O') continue;
int new_w = w;
while (new_w < map.width - 1 && line[new_w + 1] == '.') new_w++;
if (new_w != w)
{
line[w] = '.';
line[new_w] = 'O';
}
}
}
}
// Evaluate a map
fn int evaluate(Map map)
{
int height = (int)map.len();
int sum = 0;
foreach (int h, line : map)
{
foreach (c : line)
{
if (c == 'O') sum += height - h;
}
}
return sum;
}
// This solution is not really that efficient, but it
// exercises the ring buffer.
fn long part2()
{
// Let's define a 256 length ring buffer.
// This one is storing the checksums for the last steps
const BUFFER_LEN = 256;
RingBuffer(<Fnv64a, BUFFER_LEN>) ring;
ring.init();
// Load the file into memory
File f = file::open("day14.txt", "rb")!!;
defer (void)f.close();
char[200][200] map_buffer;
int height = 0;
int width @noinit;
while (try line = io::treadline(&f))
{
width = line.len;
map_buffer[height++][0:line.len] = line;
}
// Create our map.
Map map = { map_buffer[:height], width };
// Did we find a cycle yet?
bool cycle_found;
const TOTAL = 1_000_000_000;
// Spin
for LOOP: (int cycle = 0; cycle < TOTAL; cycle++)
{
// Actual spin
for (int i = 0; i < 4; i++)
{
switch (i)
{
case 0: tilt_north(map);
case 1: tilt_west(map);
case 2: tilt_south(map);
case 3: tilt_east(map);
default: unreachable();
}
}
// If we found the cycle, then we're done.
if (cycle_found) continue;
// Let's calculate a checksum *assuming* that this
// will be sufficiently unique that we don't need
// the full comparison! A bit of cheating, but probably
// ok if our hash function is good.
Fnv64a checksum;
checksum.init();
// Do the checksum.
foreach (line : map)
{
checksum.update(line);
}
for (int i = 0; i < ring.written; i++)
{
// Did we find the checksum in the ring buffer?
if (ring.getc(i) == checksum)
{
// Yes we did! Calculate the cycle length.
int cycle_length = (int)ring.written - i;
// How many cycles are left?
int cycles_left = TOTAL - cycle - 1;
// Let's add enough repetitions that we only
// need the last few iterations.
// We could also actually store the value
// for the next cycles backwards. In that case we'd be done now!
int add = (cycles_left / cycle_length) * cycle_length;
cycle += add;
cycle_found = true;
// Exit, since we don't need to push / pop to the buffer.
continue LOOP;
}
}
// Pop from the end if we have too many.
if (ring.written == BUFFER_LEN - 1)
{
ring.popc()!!;
}
// Push the checksum.
ring.putc(checksum);
}
// Finally we evaluate the result.
return evaluate(map);
}