summaryrefslogtreecommitdiff
path: root/01.c
diff options
context:
space:
mode:
authorVincent Douillet <vincent@vdouillet.fr>2021-12-03 17:01:37 +0100
committerVincent Douillet <vincent@vdouillet.fr>2021-12-03 17:01:37 +0100
commit736823f313bd2e00e49a1b52aaf0ea68a79db438 (patch)
tree5cf54f57e5c2afd8ea22baacb3ffa05a76a59fcc /01.c
parent04e79fa276ae1c3620868d85941b2c7b7c11222a (diff)
improve input handling & merge part 1 and 2 for the first days
Diffstat (limited to '01.c')
-rw-r--r--01.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/01.c b/01.c
new file mode 100644
index 0000000..33c52aa
--- /dev/null
+++ b/01.c
@@ -0,0 +1,43 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include "input.h"
+
+#define INPUT "input/01.txt"
+
+void part1(struct input_int* input) {
+ // compute depth variations
+ int result = 0;
+ for(size_t i = 1; i < input->line_count; i++) {
+ if(input->lines[i] > input->lines[i-1])
+ result++;
+ }
+ printf("%d\n", result);
+}
+
+void part2(struct input_int* input) {
+ // compute depth variations
+ int result = 0;
+ int windowSum = input->lines[0] + input->lines[1] + input->lines[2];
+ for(size_t i = 1; i < input->line_count - 2; i++) {
+ int newWindowSum = input->lines[i] + input->lines[i+1] + input->lines[i+2];
+ if(newWindowSum > windowSum)
+ result++;
+
+ windowSum = newWindowSum;
+ }
+ printf("%d\n", result);
+}
+
+int main() {
+ // read input
+ struct input_int input;
+ input_int_read(&input, INPUT);
+
+ // do stuff
+ part1(&input);
+ part2(&input);
+
+ // cleanup & exit
+ input_int_free(&input);
+ return 0;
+}