part 2 day 1
This commit is contained in:
parent
1e57445312
commit
44500bb0d1
69
01/main.c
69
01/main.c
@ -1,12 +1,12 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
main()
|
||||
void
|
||||
first()
|
||||
{
|
||||
int prev = -1;
|
||||
int total = 0;
|
||||
|
||||
|
||||
static char buffer[1024];
|
||||
FILE *f = fopen("input", "r");
|
||||
|
||||
@ -18,9 +18,70 @@ main()
|
||||
prev = val;
|
||||
}
|
||||
|
||||
printf("total increasing: %d\n", total);
|
||||
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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user