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 <utility>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
#include <intcode/intcode.hpp>
|
#include <intcode/intcode.hpp>
|
||||||
using namespace intcode;
|
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 {
|
auto Interact(Ethernet & ethernet, Machine & m, std::optional<Packet> p) -> void {
|
||||||
while(Advance(m,
|
std::visit(overloaded{
|
||||||
[&](auto &i) {
|
[&](Input i) {
|
||||||
if (p) {
|
if (p) {
|
||||||
i = p->first;
|
i.pos = p->first;
|
||||||
StepInput(m, p->second);
|
StepInput(m, p->second);
|
||||||
p = {};
|
Interact(ethernet, m, {});
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
i = -1; // no packet
|
i.pos = -1; // no packet
|
||||||
return false;
|
|
||||||
},
|
},
|
||||||
[&](auto d) {
|
[&](Output d) {
|
||||||
auto x = StepOutput(m);
|
auto x = StepOutput(m);
|
||||||
auto y = StepOutput(m);
|
auto y = StepOutput(m);
|
||||||
ethernet.push_back({d, {x,y}});
|
ethernet.push_back({d.val, {x,y}});
|
||||||
return true;
|
Interact(ethernet, m, p);
|
||||||
},
|
},
|
||||||
[]() -> bool { throw std::runtime_error{"unexpected halt"}; }
|
[](Halt) -> void { throw std::runtime_error{"unexpected halt"}; },
|
||||||
)) {}
|
}, Step(m));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
Loading…
Reference in New Issue
Block a user