From 7b493f2420728995a3a9d238068dd68428498769 Mon Sep 17 00:00:00 2001 From: eriedaberrie Date: Thu, 7 Dec 2023 21:31:28 -0800 Subject: [PATCH] 2023 day 8 --- 2023/8/main-1.cpp | 29 +++++++++++++++++++++++++ 2023/8/main-2.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 2023/8/main-1.cpp create mode 100644 2023/8/main-2.cpp diff --git a/2023/8/main-1.cpp b/2023/8/main-1.cpp new file mode 100644 index 0000000..1ac633a --- /dev/null +++ b/2023/8/main-1.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; + +int main(void) { + ios::sync_with_stdio(0); + cin.tie(0); + + string instructions; + getline(cin, instructions); + cin.ignore(1); + + unordered_map> locs; + + string line; + while (getline(cin, line)) { + locs[line.substr(0, 3)] = make_pair(line.substr(7, 3), line.substr(12, 3)); + } + + string current = "AAA"; + int i = 0; + while (current != "ZZZ") { + if (instructions[i++ % instructions.length()] == 'L') + current = locs[current].first; + else + current = locs[current].second; + } + + cout << i << '\n'; +} diff --git a/2023/8/main-2.cpp b/2023/8/main-2.cpp new file mode 100644 index 0000000..1674517 --- /dev/null +++ b/2023/8/main-2.cpp @@ -0,0 +1,54 @@ +#include +using namespace std; + +int main(void) { + ios::sync_with_stdio(0); + cin.tie(0); + + string instructions; + getline(cin, instructions); + cin.ignore(1); + + unordered_map> locs; + + string line; + while (getline(cin, line)) { + locs[line.substr(0, 3)] = make_pair(line.substr(7, 3), line.substr(12, 3)); + } + + vector starts; + + for (auto& loc : locs) { + if (loc.first[2] == 'A') + starts.push_back(loc.first); + } + + unsigned long amounts[starts.size()]; + memset(amounts, 0, starts.size() * sizeof(unsigned long)); + + long n = 0; + unsigned int done = starts.size(); + while (done > 0) { + for (unsigned int i = 0; i < starts.size(); i++) { + if (amounts[i] != 0) + continue; + + if (instructions[n % instructions.length()] == 'L') + starts[i] = locs[starts[i]].first; + else + starts[i] = locs[starts[i]].second; + if (starts[i][2] == 'Z') { + amounts[i] = n + 1; + done--; + } + } + n++; + } + + unsigned long val = amounts[0]; + for (unsigned int i = 1; i < starts.size(); i++) { + val = lcm(val, amounts[i]); + } + + cout << val << '\n'; +}