adventofcode/2021/1/depth2.c
2022-12-01 21:02:00 +01:00

56 lines
1.0 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define WINDOW_SIZE 3
int main() {
FILE *input = fopen("input", "r");
if(input == NULL) {
printf("oh noes couldn't open the input file lol\n");
return 1;
}
char line[255];
int index = 0, count = 0, last_num = INT_MAX;
int windows[WINDOW_SIZE][WINDOW_SIZE];
while(fgets(line, sizeof(line), input) != NULL) {
int cur_num = atoi(line),
cur_index = index % (WINDOW_SIZE);
// Put values diagonal into the 3x3 grid, like this
// A B C
// B C A
// C A B
for(int i=0; i<WINDOW_SIZE; i++){
int cur_col = (cur_index + i) % WINDOW_SIZE;
windows[i][cur_col] = cur_num;
}
index++;
if(index < WINDOW_SIZE) // skip first 3 values
continue;
// Calculate average for current column, e.g.
// A B C
// B C A
// C A B
// ^
int sum = 0;
for(int i=0; i<WINDOW_SIZE; i++)
sum += windows[cur_index][i];
count += sum > last_num;
last_num = sum;
}
fclose(input);
printf("There were %d measurements larger than the last.\n", count);
}