Compare commits
No commits in common. '86120c01a103d70937981a3fcad3785e3ebc77e6' and '37d59142ab4901cb22214ccaa2ec342925644c46' have entirely different histories.
86120c01a1
...
37d59142ab
7 changed files with 10 additions and 161 deletions
@ -1,84 +1,9 @@ |
||||
#include "Day08.h" |
||||
|
||||
Result Day08::Task1() { |
||||
Map map = parseMap(); |
||||
vector<uint8_t> instructions = parseInstructions(); |
||||
|
||||
uint64 steps = 0; |
||||
size_t instructionIndex = 0; |
||||
string node = "AAA"; |
||||
while (node != "ZZZ"){ |
||||
node = map[node][instructions[instructionIndex]]; |
||||
steps++; |
||||
instructionIndex = (++instructionIndex) % instructions.size(); |
||||
} |
||||
|
||||
return to_string(steps); |
||||
return Day::Task1(); |
||||
} |
||||
|
||||
Result Day08::Task2() { |
||||
Map map = parseMap(); |
||||
vector<uint8_t> instructions = parseInstructions(); |
||||
|
||||
vector<string> nodes; |
||||
for (auto [node, path] : map) |
||||
if (node.ends_with('A')) |
||||
nodes.push_back(node); |
||||
|
||||
vector<uint64> allSteps; |
||||
|
||||
for (string &node : nodes){ |
||||
uint64 steps = 0; |
||||
size_t instructionIndex = 0; |
||||
|
||||
while (!node.ends_with('Z')){ |
||||
node = map[node][instructions[instructionIndex]]; |
||||
steps++; |
||||
instructionIndex = (++instructionIndex) % instructions.size(); |
||||
} |
||||
|
||||
allSteps.push_back(steps); |
||||
} |
||||
|
||||
uint64 lcm = 1; |
||||
for (uint64 steps : allSteps) |
||||
lcm = LCM(lcm, steps); |
||||
|
||||
return to_string(lcm); |
||||
} |
||||
|
||||
Day08::Map Day08::parseMap() const { |
||||
Map map; |
||||
|
||||
for (size_t i = 2; i < input.size(); i++){ |
||||
string line = input[i]; |
||||
string source = line.substr(0, 3); |
||||
string left = line.substr(7, 3); |
||||
string right = line.substr(12, 3); |
||||
map[source] = {left, right}; |
||||
} |
||||
|
||||
return map; |
||||
} |
||||
|
||||
vector<uint8_t> Day08::parseInstructions() const { |
||||
vector<uint8_t> instructions; |
||||
|
||||
for (const char c : input[0]) |
||||
instructions.push_back(c == 'L' ? 0 : 1); |
||||
|
||||
return instructions; |
||||
} |
||||
|
||||
uint64 Day08::GCD(uint64 a, uint64 b) { |
||||
while (b != 0){ |
||||
uint64 t = b; |
||||
b = a % b; |
||||
a = t; |
||||
} |
||||
return a; |
||||
} |
||||
|
||||
uint64 Day08::LCM(uint64 a, uint64 b) { |
||||
return a * b / GCD(a, b); |
||||
return Day::Task2(); |
||||
} |
@ -1,47 +1,9 @@ |
||||
#include "Day09.h" |
||||
|
||||
Result Day09::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); |
||||
return Day::Task1(); |
||||
} |
||||
|
||||
Result Day09::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; |
||||
return Day::Task2(); |
||||
} |
Loading…
Reference in new issue