diff --git a/day23.cpp b/day23.cpp index 897de7a..833c53f 100644 --- a/day23.cpp +++ b/day23.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include using namespace intcode; @@ -25,25 +26,23 @@ auto BuildNetwork(Machine m) -> std::vector { } auto Interact(Ethernet & ethernet, Machine & m, std::optional p) -> void { - while(Advance(m, - [&](auto &i) { + std::visit(overloaded{ + [&](Input i) { if (p) { - i = p->first; + i.pos = p->first; StepInput(m, p->second); - p = {}; - return true; + Interact(ethernet, m, {}); } - i = -1; // no packet - return false; + i.pos = -1; // no packet }, - [&](auto d) { + [&](Output d) { auto x = StepOutput(m); auto y = StepOutput(m); - ethernet.push_back({d, {x,y}}); - return true; + ethernet.push_back({d.val, {x,y}}); + Interact(ethernet, m, p); }, - []() -> bool { throw std::runtime_error{"unexpected halt"}; } - )) {} + [](Halt) -> void { throw std::runtime_error{"unexpected halt"}; }, + }, Step(m)); } } // namespace