You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.3 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define INPUT_FILE ("input.txt")
#define ARR_LEN (2000)
int main(int argc, char** argv)
{
int measurements[ARR_LEN];
memset(measurements, 0, ARR_LEN * sizeof(int));
FILE* fp = fopen(INPUT_FILE, "r");
if(fp == NULL)
{
printf("File failed to open for reading...");
exit(-1);
}
char* line = NULL;
size_t len = 0;
ssize_t rc = 0;
uint16_t ct = 0;
// counter that keeps track of how many times a depth measurement has increased
int increase_count = 0;
if((rc = getline(&line, &len, fp)) < 0)
{
printf("Bad read...\n");
exit(-1);
}
else
{
measurements[ct++] = strtol(line, NULL, 10);
}
while((rc = getline(&line, &len, fp)) >= 0)
{
measurements[ct] = strtol(line, NULL, 10);
if(measurements[ct] - measurements[ct - 1] > 0)
{
increase_count++;
}
ct++;
}
printf("Increase count: %d\n", increase_count);
// part 2
int sum_increase_count = 0;
for(int ind = 2; ind < ARR_LEN - 1; ind++)
{
int sum_one = measurements[ind - 2] +
measurements[ind - 1] +
measurements[ind];
int sum_two = measurements[ind - 1] +
measurements[ind] +
measurements[ind + 1];
sum_increase_count += (sum_two > sum_one);
}
printf("Sum increase count: %d\n", sum_increase_count);
return 0;
}