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