89 lines
1.3 KiB
C
89 lines
1.3 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
#define INPUT "../inputs/input-day1"
|
|
|
|
void
|
|
first()
|
|
{
|
|
int prev = -1;
|
|
int total = 0;
|
|
|
|
static char buffer[1024];
|
|
FILE *f = fopen(INPUT, "r");
|
|
|
|
while(fgets(buffer, 1024, f)) {
|
|
int val = atoi(buffer);
|
|
if(prev != -1 && val > prev) {
|
|
total++;
|
|
}
|
|
prev = val;
|
|
}
|
|
|
|
printf("part 1: %d\n", total);
|
|
|
|
fclose(f);
|
|
}
|
|
|
|
int
|
|
count_lines()
|
|
{
|
|
int total = 0;
|
|
static char buf[256];
|
|
FILE *f = fopen(INPUT, "r");
|
|
while(fgets(buf, 256, f))
|
|
total++;
|
|
fclose(f);
|
|
return total;
|
|
}
|
|
|
|
int*
|
|
load_all(int count)
|
|
{
|
|
int *i = calloc(sizeof(int), count);
|
|
|
|
int idx = 0;
|
|
static char buf[256];
|
|
FILE *f = fopen(INPUT, "r");
|
|
|
|
while(fgets(buf, 256, f)) {
|
|
i[idx] = atoi(buf);
|
|
idx++;
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return i;
|
|
}
|
|
|
|
void
|
|
second()
|
|
{
|
|
int count = count_lines();
|
|
int *values = load_all(count);
|
|
|
|
int totals = 0;
|
|
int lastsum = -1;
|
|
|
|
for(int i = 0; i < count; ++i) {
|
|
if(i + 2 < count) {
|
|
int sum = values[i] + values[i+1] + values[i+2];
|
|
if(lastsum != -1 && sum > lastsum) {
|
|
totals++;
|
|
}
|
|
lastsum = sum;
|
|
}
|
|
}
|
|
|
|
printf("part 2: %d\n", totals);
|
|
|
|
free(values);
|
|
}
|
|
|
|
int
|
|
main()
|
|
{
|
|
first();
|
|
second();
|
|
return 0;
|
|
} |