advance
This commit is contained in:
parent
d96eafaa47
commit
d6727ab770
19
day23.cpp
19
day23.cpp
|
@ -25,28 +25,25 @@ 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(std::visit(overloaded{
|
while(Advance(m,
|
||||||
|
[&](auto &i) {
|
||||||
[&](Input i) {
|
|
||||||
if (p) {
|
if (p) {
|
||||||
i.pos = p->first;
|
i = p->first;
|
||||||
StepInput(m, p->second);
|
StepInput(m, p->second);
|
||||||
p = {};
|
p = {};
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
i.pos = -1; // no packet
|
i = -1; // no packet
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
|
[&](auto d) {
|
||||||
[&](Output o) {
|
|
||||||
auto x = StepOutput(m);
|
auto x = StepOutput(m);
|
||||||
auto y = StepOutput(m);
|
auto y = StepOutput(m);
|
||||||
ethernet.push_back({o.val, {x,y}});
|
ethernet.push_back({d, {x,y}});
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
|
[]() -> bool { throw std::runtime_error{"unexpected halt"}; }
|
||||||
[](Halt) -> bool { throw std::runtime_error{"unexpected halt"}; },
|
)) {}
|
||||||
}, Step(m))) {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#define INTCODE_INTERPRETER_HPP_
|
#define INTCODE_INTERPRETER_HPP_
|
||||||
|
|
||||||
#include <variant>
|
#include <variant>
|
||||||
|
#include <concepts>
|
||||||
|
|
||||||
#include <intcode/Machine.hpp>
|
#include <intcode/Machine.hpp>
|
||||||
#include <Overload.hpp>
|
#include <Overload.hpp>
|
||||||
|
@ -27,19 +28,22 @@ struct BadInstruction : public std::runtime_error {
|
||||||
explicit BadInstruction(char const* what);
|
explicit BadInstruction(char const* what);
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class Fin, class Fout>
|
template <std::invocable<ValueType&> Fin, std::invocable<ValueType> Fout, std::invocable<> Fhalt>
|
||||||
|
auto Advance(Machine & machine, Fin input, Fout output, Fhalt halt) {
|
||||||
|
return std::visit(overloaded{
|
||||||
|
[&](Halt) { return halt(); },
|
||||||
|
[&](Input i) { return input(i.pos); },
|
||||||
|
[&](Output o) { return output(o.val); },
|
||||||
|
}, Step(machine));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::invocable<> Fin, std::invocable<ValueType> Fout>
|
||||||
auto Run(Machine & machine, Fin input, Fout output) -> void {
|
auto Run(Machine & machine, Fin input, Fout output) -> void {
|
||||||
while (std::visit(overloaded{
|
while (Advance(machine,
|
||||||
[](Halt) { return false; },
|
[&](ValueType &i) { i = input(); return true; },
|
||||||
[&](Input arg) {
|
[&](ValueType o) { output(o); return true; },
|
||||||
arg.pos = input();
|
[]() { return false; }
|
||||||
return true;
|
)) {}
|
||||||
},
|
|
||||||
[&](Output arg) {
|
|
||||||
output(arg.val);
|
|
||||||
return true;
|
|
||||||
}},
|
|
||||||
Step(machine)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
Loading…
Reference in New Issue
Block a user