rename input/output

This commit is contained in:
Eric Mertens 2022-11-06 19:46:43 -08:00
parent 6a34b0f309
commit 041c055f43
4 changed files with 10 additions and 10 deletions

View File

@ -24,7 +24,7 @@ auto Feed(R &&amps, ValueType start) -> std::optional<ValueType> {
for (auto &m : amps) { for (auto &m : amps) {
auto e = Step(m); auto e = Step(m);
if (auto i = std::get_if<Input>(&e)) { if (auto i = std::get_if<Input>(&e)) {
i->pos = start; i->input = start;
start = StepOutput(m); start = StepOutput(m);
} else { } else {
return {}; return {};

View File

@ -19,7 +19,7 @@ using Ethernet = std::deque<Packet>;
auto BuildNetwork(Machine m) -> std::vector<Machine> { auto BuildNetwork(Machine m) -> std::vector<Machine> {
std::vector<Machine> machines; std::vector<Machine> machines;
auto& i = std::get<Input>(Step(m)).pos; auto& i = std::get<Input>(Step(m)).input;
for (i = 0; i < 50; i++) { for (i = 0; i < 50; i++) {
machines.push_back(m); machines.push_back(m);
} }
@ -32,7 +32,7 @@ auto Interact(Ethernet & ethernet, Machine & m, std::optional<Payload> p) -> voi
switch (e.index()) { switch (e.index()) {
default: throw std::runtime_error{"unexpected halt"}; default: throw std::runtime_error{"unexpected halt"};
case 0: { case 0: {
auto& i = std::get<0>(e).pos; auto& i = std::get<0>(e).input;
if (p) { if (p) {
i = p->x; i = p->x;
StepInput(m, p->y); StepInput(m, p->y);
@ -43,7 +43,7 @@ auto Interact(Ethernet & ethernet, Machine & m, std::optional<Payload> p) -> voi
return; return;
} }
case 1: { case 1: {
auto d = std::get<1>(e).val; auto d = std::get<1>(e).output;
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, {x,y}});

View File

@ -9,11 +9,11 @@
namespace intcode { namespace intcode {
struct Input { struct Input {
ValueType &pos; ValueType &input;
}; };
struct Output { struct Output {
ValueType val; ValueType output;
}; };
struct Halt {}; struct Halt {};
@ -32,8 +32,8 @@ template <std::invocable<ValueType&> Fin, std::invocable<ValueType> Fout, std::i
auto Advance(Machine & machine, Fin input, Fout output, Fhalt halt) { auto Advance(Machine & machine, Fin input, Fout output, Fhalt halt) {
return std::visit(overloaded{ return std::visit(overloaded{
[&](Halt) { return halt(); }, [&](Halt) { return halt(); },
[&](Input i) { return input(i.pos); }, [&](Input i) { return input(i.input); },
[&](Output o) { return output(o.val); }, [&](Output o) { return output(o.output); },
}, Step(machine)); }, Step(machine));
} }

View File

@ -6,11 +6,11 @@ BadInstruction::BadInstruction(char const* what)
: std::runtime_error{what} {} : std::runtime_error{what} {}
auto StepInput(Machine & m, ValueType input) -> void { auto StepInput(Machine & m, ValueType input) -> void {
std::get<Input>(Step(m)).pos = input; std::get<Input>(Step(m)).input = input;
} }
auto StepOutput(Machine & m) -> ValueType { auto StepOutput(Machine & m) -> ValueType {
return std::get<Output>(Step(m)).val; return std::get<Output>(Step(m)).output;
} }
auto Step(Machine & m) -> std::variant<Input, Output, Halt> { auto Step(Machine & m) -> std::variant<Input, Output, Halt> {