parent
3c5d23433b
commit
86120c01a1
2 changed files with 43 additions and 3 deletions
@ -1,9 +1,47 @@ |
|||||||
#include "Day09.h" |
#include "Day09.h" |
||||||
|
|
||||||
Result Day09::Task1() { |
Result Day09::Task1() { |
||||||
return Day::Task1(); |
int64 sum = 0; |
||||||
|
for (auto &history : parseHistories()){ |
||||||
|
vector<int64> tails = {history.back()}; |
||||||
|
vector<int64> diff = history; |
||||||
|
|
||||||
|
while (std::any_of(diff.begin(), diff.end(), [](int64 v){return v != 0;})){ |
||||||
|
for (size_t i = 0; i < diff.size() - 1; i++) |
||||||
|
diff[i] = diff[i + 1] - diff[i]; |
||||||
|
diff.pop_back(); |
||||||
|
tails.push_back(diff.back()); |
||||||
|
} |
||||||
|
|
||||||
|
sum += std::reduce(tails.begin(), tails.end(), 0, [](int64 a, int64 b){ return a + b; }); |
||||||
|
} |
||||||
|
return to_string(sum); |
||||||
} |
} |
||||||
|
|
||||||
Result Day09::Task2() { |
Result Day09::Task2() { |
||||||
return Day::Task2(); |
int64 sum = 0; |
||||||
} |
for (auto &history : parseHistories()){ |
||||||
|
vector<int64> heads = {history.front()}; |
||||||
|
vector<int64> diff = history; |
||||||
|
|
||||||
|
while (std::any_of(diff.begin(), diff.end(), [](int64 v){return v != 0;})){ |
||||||
|
for (size_t i = 0; i < diff.size() - 1; i++) |
||||||
|
diff[i] = diff[i + 1] - diff[i]; |
||||||
|
diff.pop_back(); |
||||||
|
heads.push_back(diff.front()); |
||||||
|
} |
||||||
|
|
||||||
|
for (size_t i = 0; i < heads.size(); i++) |
||||||
|
sum += heads[i] * (i % 2 == 0 ? 1 : -1); |
||||||
|
} |
||||||
|
return to_string(sum); |
||||||
|
} |
||||||
|
|
||||||
|
vector<vector<int64>> Day09::parseHistories() const { |
||||||
|
vector<vector<int64>> histories; |
||||||
|
|
||||||
|
for (const string &line : input) |
||||||
|
histories.push_back(parseInts(line)); |
||||||
|
|
||||||
|
return histories; |
||||||
|
} |
||||||
|
Loading…
Reference in new issue