day1.c (937B)
- #include <assert.h> /* assert() */
- #include <fenv.h> /* fesetround(), FE_DOWNWARD */
- #include <math.h> /* lround() */
- #include <stdio.h> /* printf(), getline() */
- #include <stdlib.h> /* atol() */
- long int
- fuel_required(long int mass)
- {
- long int result;
- fesetround(FE_DOWNWARD);
- result = (lround(mass / 3) - 2);
- 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(void)
- {
- char *line_mass = NULL;
- size_t len = 0;
- long int modules_total = 0, grand_total = 0, module_fuel;
- while(getline(&line_mass, &len, stdin) != -1)
- {
- module_fuel = fuel_required(atol(line_mass));
- modules_total += module_fuel;
- grand_total += module_fuel + fuel_fuel(module_fuel);
- }
- printf("Modules fuel: %ld\n", modules_total);
- printf("Grand total: %ld\n", grand_total);
- return 0;
- }