advent-of-code/2019/01/day1.c

31 lines
596 B
C
Raw Permalink Normal View History

2019-12-01 19:13:58 +01:00
#include <stdio.h>
#include <stdlib.h>
int cost(int mass) {
return mass / 3 - 2;
}
int costRec(int mass, int acc) {
int c = cost(mass);
if (c <= 0) {
return acc;
}
return costRec(c, acc+c);
}
2019-12-01 21:04:00 +01:00
int main(int argc, char *argv[]) {
2019-12-01 19:13:58 +01:00
FILE* inputFile = fopen("input", "r");
char line [10];
int fuel = 0;
2019-12-01 21:04:00 +01:00
int fuelRec = 0;
2019-12-01 19:13:58 +01:00
while (fgets(line, sizeof(line), inputFile)) {
fuel += cost(atoi(line));
2019-12-01 21:04:00 +01:00
fuelRec += costRec(atoi(line), 0);
2019-12-01 19:13:58 +01:00
}
fclose(inputFile);
2019-12-01 21:04:00 +01:00
printf("Part 1: %d\n", fuel);
printf("Part 2: %d\n", fuelRec);
2019-12-01 19:13:58 +01:00
}