-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
84 lines (70 loc) · 1.79 KB
/
main.cpp
File metadata and controls
84 lines (70 loc) · 1.79 KB
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
#include "AoC64.h"
#include "stack.h"
#if __has_include("input.h")
#include "input.h" // user’s private input (gitignored)
#else
#include "input.example.h" // checked-in sample input
#endif
struct Cucumber {
uint8_t x;
uint8_t y;
};
Stack<Cucumber, uint16_t, 5000> cucumbers;
inline const uint8_t east(const uint8_t x) {
uint8_t nx = x + 1;
if (nx == dimx)
return 0;
return nx;
}
inline const uint8_t down(const uint8_t y) {
uint8_t ny = y + 1;
if (ny == dimy)
return 0;
return ny;
}
int8_t step() {
// east facing cucumbers
cucumbers.clear();
for (uint8_t i = 0; i < dimy; ++i) {
for (uint8_t j = 0; j < dimx; ++j) {
if (input[i][j] != '>')
continue;
if (input[i][east(j)] == '.')
cucumbers.push(Cucumber {j, i});
}
}
const uint16_t m1 = cucumbers.size();
for (uint16_t k = 0; k < m1; ++k) {
const Cucumber& c = cucumbers.arr[k];
input[c.y][c.x] = '.';
input[c.y][east(c.x)] = '>';
}
// down facing cucumbers
cucumbers.clear();
for (uint8_t i = 0; i < dimy; ++i) {
for (uint8_t j = 0; j < dimx; ++j) {
if (input[i][j] != 'v')
continue;
if (input[down(i)][j] == '.')
cucumbers.push(Cucumber {j, i});
}
}
const uint16_t m2 = cucumbers.size();
for (uint16_t k = 0; k < m2; ++k) {
const Cucumber& c = cucumbers.arr[k];
input[c.y][c.x] = '.';
input[down(c.y)][c.x] = 'v';
}
return m1 > 0 || m2 > 0;
}
int main(void) {
init(25);
uint8_t t = 0;
uint16_t steps = 0;
do {
tick(t++ & (uint8_t)7);
++steps;
} while (step());
printf("part 1: %d\n", steps);
finish();
}