diff --git a/day07.cpp b/day07.cpp index e34911e..79e97a4 100644 --- a/day07.cpp +++ b/day07.cpp @@ -22,9 +22,9 @@ template auto Controller(Machine machine, R const ¶ms) { template auto Feed(R &&s, ValueType start) -> std::optional { for (auto &m : amps) { - auto i = Step(m); - if (auto p = std::get_if(&i)) { - p->pos = start; + auto e = Step(m); + if (auto i = std::get_if(&e)) { + i->pos = start; start = StepOutput(m); } else { return {}; diff --git a/day23.cpp b/day23.cpp index 01fd7f2..897de7a 100644 --- a/day23.cpp +++ b/day23.cpp @@ -16,10 +16,10 @@ using Packet = std::pair; using Ethernet = std::deque>; auto BuildNetwork(Machine m) -> std::vector { + auto& i = std::get(Step(m)).pos; std::vector machines; - for (int i = 0; i < 50; i++) { + for (i = 0; i < 50; i++) { machines.push_back(m); - StepInput(machines.back(), i); } return machines; } @@ -57,26 +57,22 @@ auto main() -> int { std::optional nat; for(;;) { - if (ethernet.empty()) { - if (nat) { - if (nat->second == part2) { break; } - part2 = nat->second; - Interact(ethernet, machines[0], nat); - } else { - for (auto && m : machines) { - Interact(ethernet, m, {}); - } - } - } else { + if (!ethernet.empty()) { auto [dest, p] = ethernet.front(); ethernet.pop_front(); if (dest == 255) { nat = p; if (!part1) { part1 = p.second; } - } else if (dest < 50) { - Interact(ethernet, machines[dest], p); } else { - throw std::runtime_error{"bad destination"}; + Interact(ethernet, machines.at(dest), p); + } + } else if (nat) { + if (nat->second == part2) { break; } + part2 = nat->second; + Interact(ethernet, machines[0], nat); + } else { + for (auto && m : machines) { + Interact(ethernet, m, {}); } } }