-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay01.c
More file actions
70 lines (49 loc) · 1.37 KB
/
Day01.c
File metadata and controls
70 lines (49 loc) · 1.37 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
#include "Helpers.c"
#define CAP 4096
static int parse(const char *input, int depths[CAP]) {
int n = 0;
int charsRead = 0;
int filled = 0;
while ((filled = sscanf(input, "%u\n%n", &depths[n++], &charsRead)) != EOF) {
assert(filled == 1 && "parseInput: Failed to parse input");
input += charsRead;
assert(n < CAP);
}
return n;
}
static int partOne(int n, const int depths[n]) {
int count = 0;
int previousDepth = 0;
for (int i = 0; i < n; ++i) {
int depth = depths[i];
if (previousDepth > 0 && previousDepth < depth) {
++count;
}
previousDepth = depth;
}
return count;
}
static int partTwo(int n, const int depths[n]) {
int count = 0;
int previousSum = 0;
for (int i = 0; i < (n / 3) * 3; ++i) {
int sum = depths[i] + depths[i + 1] + depths[i + 2];
if (previousSum > 0 && previousSum < sum) {
++count;
}
previousSum = sum;
}
return count;
}
int main() {
const char *input = Helpers_readInputFile(__FILE__);
int depths[CAP] = {0};
int n = parse(input, depths);
Helpers_assert(PART1, Helpers_clock(),
partOne(n, depths),
7, 1139);
Helpers_assert(PART2, Helpers_clock(),
partTwo(n, depths),
5, 1103);
return 0;
}