Try tail recursion
This commit is contained in:
parent
e23ce1f101
commit
6b4c7c7a2f
23
day23.cpp
23
day23.cpp
@ -6,6 +6,7 @@
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
|
||||
#include <intcode/intcode.hpp>
|
||||
using namespace intcode;
|
||||
@ -25,25 +26,23 @@ auto BuildNetwork(Machine m) -> std::vector<Machine> {
|
||||
}
|
||||
|
||||
auto Interact(Ethernet & ethernet, Machine & m, std::optional<Packet> 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
|
||||
|
Loading…
Reference in New Issue
Block a user