diff options
Diffstat (limited to '2023/09/src/part2.cpp')
-rw-r--r-- | 2023/09/src/part2.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/2023/09/src/part2.cpp b/2023/09/src/part2.cpp new file mode 100644 index 0000000..df1ec5a --- /dev/null +++ b/2023/09/src/part2.cpp | |||
@@ -0,0 +1,45 @@ | |||
1 | #include <numeric> | ||
2 | #include <fstream> | ||
3 | #include <iostream> | ||
4 | #include <sstream> | ||
5 | #include <vector> | ||
6 | |||
7 | void solve(std::vector<int> v, int& answer) | ||
8 | { | ||
9 | for (int a = 0; a < v.size() - 1; ++a) | ||
10 | { | ||
11 | answer += v[a]; | ||
12 | for (int b = v.size() - 1; b > a; --b) | ||
13 | { | ||
14 | v[b] = v[b-1] - v[b]; | ||
15 | } | ||
16 | } | ||
17 | } | ||
18 | |||
19 | int main(int argc, char* argv[]) | ||
20 | { | ||
21 | int answer{}; | ||
22 | |||
23 | std::ifstream input{ argv[1] }; | ||
24 | if (input.is_open()) | ||
25 | { | ||
26 | std::string line; | ||
27 | while (not std::getline(input,line).eof()) | ||
28 | { | ||
29 | int n; | ||
30 | std::vector<int> v{}; | ||
31 | std::istringstream sline{ line }; | ||
32 | while(sline >> n) | ||
33 | { | ||
34 | v.push_back(n); | ||
35 | } | ||
36 | |||
37 | solve(std::move(v), answer); | ||
38 | } | ||
39 | } | ||
40 | input.close(); | ||
41 | |||
42 | std::cout << answer << std::endl; | ||
43 | return 0; | ||
44 | } | ||
45 | |||