summaryrefslogtreecommitdiff
path: root/01.c
blob: 33c52aa57c53639aa1560c9f2f949cccad61c658 (plain)
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
#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;
}