commit: b56a2f04ed18c3d0d149eb2a9126b3b38f45f797
parent: 038544a5c4ee25bce18281634fd6311fcd73631d
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Sun, 1 Dec 2019 19:32:07 +0100
day1: part2
Diffstat:
M | day1.c | 32 | +++++++++++++++++++------------- |
1 file changed, 19 insertions(+), 13 deletions(-)
diff --git a/day1.c b/day1.c
@@ -2,35 +2,41 @@
#include <fenv.h> /* fesetround(), FE_DOWNWARD */
#include <assert.h> /* assert() */
#include <stdio.h> /* printf() */
-#include <stdlib.h> /* atoi() */
+#include <stdlib.h> /* atol() */
-long int fuel_required(int mass) {
+long int fuel_required(long int mass) {
long int result;
fesetround(FE_DOWNWARD);
result = (lround(mass/3)-2);
- printf("%ld\n", result);
-
return result;
}
+long int fuel_fuel(long int input) {
+ long int result;
+
+ result = fuel_required(input);
+
+ if(result <= 0)
+ return 0;
+ else
+ return result + fuel_fuel(result);
+}
+
int main(int argc, char *argv[]) {
char *line_mass = NULL;
size_t len = 0;
- long int total = 0;
+ long int modules_total = 0, grand_total = 0, module_fuel;
while(getline(&line_mass, &len, stdin) != -1) {
- total += fuel_required(atoi(line_mass));
+ module_fuel = fuel_required(atol(line_mass));
+ modules_total += module_fuel;
+ grand_total += module_fuel + fuel_fuel(module_fuel);
}
- printf("\nTotal: %ld\n", total);
-
- /*
- * for(i;i<argc;i++) {
- * fuel_required(atoi(argv[i]));
- * }
- */
+ printf("Modules fuel: %ld\n", modules_total);
+ printf("Grand total: %ld\n", grand_total);
return 0;
}