-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9.cpp
64 lines (57 loc) · 1.52 KB
/
9.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
std::int64_t rec(const std::vector<std::int64_t>& seq) {
bool all_zeros = true;
for (const auto& x : seq) {
if (x != 0) {
all_zeros = false;
break;
}
}
if (all_zeros) return 0;
std::vector<std::int64_t> new_seq;
for (int i = 1; i < seq.size(); ++i) {
new_seq.push_back(seq[i] - seq[i - 1]);
}
return seq.back() + rec(new_seq);
}
std::int64_t part1(const std::vector<std::vector<std::int64_t>>& seqs) {
std::int64_t ans = 0;
for (const auto& seq : seqs) {
ans += rec(seq);
}
return ans;
}
std::int64_t part2(const std::vector<std::vector<std::int64_t>>& seqs) {
std::int64_t ans = 0;
for (auto seq : seqs) {
std::reverse(seq.begin(), seq.end());
ans += rec(seq);
}
return ans;
}
int main() {
std::ifstream f("9.txt");
std::string line;
std::vector<std::vector<std::int64_t>> seqs;
while (std::getline(f, line)) {
std::int64_t sign = 1;
std::int64_t cur = 0;
seqs.push_back({});
for (const auto& c : line) {
if (std::isdigit(c)) {
cur = cur * 10 + (c - '0');
} else if (c == '-') {
sign = -1;
} else {
seqs.back().push_back(sign * cur);
cur = 0;
sign = 1;
}
}
seqs.back().push_back(sign * cur);
}
std::cout << part2(seqs) << std::endl;
}