-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
48 lines (41 loc) · 979 Bytes
/
main.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
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <vector>
std::size_t stage1(const std::vector<int>& input) {
std::size_t sum{};
for (auto el : input) {
sum += (el / 3) - 2;
}
return sum;
}
std::size_t stage2(const std::vector<int>& input) {
std::size_t sum{};
auto get_fuel = [](auto mass) {
return (mass / 3) - 2;
};
for (auto el : input) {
for (;;) {
el = get_fuel(el);
if (el <= 0)
break;
sum += el;
}
}
return sum;
}
int main() {
std::ifstream input_file("./input.txt");
if (not input_file.good()) {
std::cerr << "Could not open the input file.\n";
return 1;
}
std::vector<int> input;
int module_mass{};
while(input_file >> module_mass) {
input.push_back(module_mass);
}
std::cout << "[Stage1] Sum of the fuel requirements: " << stage1(input) << std::endl;
std::cout << "[Stage2] Sum of the fuel requirements: " << stage2(input) << std::endl;
return 0;
}