-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_01b.cpp
35 lines (32 loc) · 822 Bytes
/
day_01b.cpp
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
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
int getRecursiveFuel(const int mass) {
constexpr int divisor = 3;
constexpr int subtract = 2;
constexpr int minimum_mass = divisor * subtract;
if (mass < minimum_mass) {
return 0;
}
const int fuel = mass / divisor - subtract;
return fuel + getRecursiveFuel(fuel);
}
int main(int argc, char* argv[]) {
// Get input
std::string input = "../input/day_01_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::istream_iterator<int> start(file), end;
const std::vector<int> masses(start, end);
// Solve
long long total_fuel = 0;
for (const auto& mass : masses) {
total_fuel += getRecursiveFuel(mass);
}
std::cout << total_fuel << '\n';
return total_fuel;
}