From 6b4c7c7a2fd8a6dc4714b72084fe9ea10b50e925 Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Sun, 6 Nov 2022 10:07:31 -0800 Subject: [PATCH] Try tail recursion --- day23.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) 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