-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday1_fuel-up.cpp
More file actions
46 lines (39 loc) · 1.29 KB
/
day1_fuel-up.cpp
File metadata and controls
46 lines (39 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cmath>
#include <cassert>
#include "utils.cpp"
int fuelForModule(const int moduleMass) {
// note : floor is useless because integer division does truncation toward zero
// https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division
return floor(moduleMass / 3) - 2;
}
int fuelForModuleFixed(const int moduleMass) {
const int initialFuel = floor(moduleMass / 3) - 2;
return initialFuel > 0 ? initialFuel + fuelForModuleFixed(initialFuel) : 0;
}
int main(int argc, char const *argv[])
{
// Part 1
assert(fuelForModule(12) == 2);
assert(fuelForModule(14) == 2);
assert(fuelForModule(1969) == 654);
assert(fuelForModule(100756) == 33583);
const auto firstInput = getPuzzleInput("./inputs/aoc_day1_1.txt");
int spaceshipFuel = 0;
for (string moduleMass : firstInput)
{
spaceshipFuel += fuelForModule(stoi(moduleMass));
}
cout << "spaceshipFuel: " << spaceshipFuel << "\n";
// Part 2
assert(fuelForModuleFixed(14) == 2);
assert(fuelForModuleFixed(1969) == 966);
assert(fuelForModuleFixed(100756) == 50346);
int spaceshipFuelFixed = 0;
for (string moduleMass : firstInput)
{
spaceshipFuelFixed += fuelForModuleFixed(stoi(moduleMass));
}
cout << "spaceshipFuelFixed: " << spaceshipFuelFixed << "\n";
return 0;
}