-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay06.c
More file actions
62 lines (48 loc) · 1.38 KB
/
Day06.c
File metadata and controls
62 lines (48 loc) · 1.38 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
#include "Helpers.c"
static void parse(const char *input, int64_t fish[9]) {
int charsRead = 0;
int filled = 0;
int timer = 0;
memset(fish, 0, 9 * sizeof(fish[0]));
while ((filled = sscanf(input, "%d,%n", &timer, &charsRead)) != EOF) {
assert(filled == 1);
assert(timer >= 0 && timer <= 8);
++fish[timer];
input += charsRead;
}
}
static int64_t simulate(const int64_t initialFish[9], int days) {
int64_t f[9] = {0};
memcpy(f, initialFish, sizeof(f));
for (int day = 0; day < days; ++day) {
int64_t zero = f[0];
f[0] = f[1];
f[1] = f[2];
f[2] = f[3];
f[3] = f[4];
f[4] = f[5];
f[5] = f[6];
f[6] = f[7] + zero;
f[7] = f[8];
f[8] = zero;
}
return f[0] + f[1] + f[2] + f[3] + f[4] + f[5] + f[6] + f[7] + f[8];
}
static int64_t partOne(const int64_t fish[9]) {
return simulate(fish, 80);
}
static int64_t partTwo(const int64_t fish[9]) {
return simulate(fish, 256);
}
int main() {
const char *input = Helpers_readInputFile(__FILE__);
int64_t fish[9] = {0};
parse(input, fish);
Helpers_assert(PART1, Helpers_clock(),
partOne(fish),
5934, 391671);
Helpers_assert(PART2, Helpers_clock(),
partTwo(fish),
26984457539, 1754000560399);
return 0;
}