This commit is contained in:
Eric Mertens 2022-11-04 11:36:30 -07:00
parent e98a71dad6
commit 8d65f74d9f
6 changed files with 48 additions and 45 deletions

View File

@ -8,17 +8,9 @@ namespace {
auto Compute(Machine machine, ValueType d) -> ValueType { auto Compute(Machine machine, ValueType d) -> ValueType {
ValueType last_output = -1; ValueType last_output = -1;
while (std::visit(overloaded{ Run(machine,
[](Halt) { return false; }, [=]() { return d; },
[d, &machine](Input arg) { [&](auto o) { last_output = o; });
arg.pos = d;
return true;
},
[&last_output](Output arg) {
last_output = arg.val;
return true;
}},
Step(machine)));
return last_output; return last_output;
} }

View File

@ -8,17 +8,9 @@ namespace {
auto Compute(Machine machine, ValueType d) -> ValueType { auto Compute(Machine machine, ValueType d) -> ValueType {
ValueType output = -1; ValueType output = -1;
while (std::visit(overloaded{ Run(machine,
[](Halt) { return false; }, [=](){ return d; },
[d](Input arg) { [&](auto o) { output = o; });
arg.pos = d;
return true;
},
[&output](Output arg) {
output = arg.val;
return false;
}},
Step(machine)));
return output; return output;
} }

View File

@ -18,26 +18,23 @@ auto Compute(Machine machine, ValueType start)
ValueType dx {0}; ValueType dx {0};
ValueType dy {-1}; ValueType dy {-1};
for (;;) { bool next_color = true;
auto effect = Step(machine);
if (std::holds_alternative<Halt>(effect)) { Run(machine,
return paint; [&]() { return paint[here]; },
} [&](auto o) {
if (next_color) {
paint[here] = o;
} else {
std::swap(dx, dy);
(o ? dx : dy) *= -1;
here.first += dx;
here.second += dy;
}
next_color = !next_color;
});
std::get<Input>(effect).pos = paint[here]; return paint;
paint[here] = std::get<Output>(Step(machine)).val;
auto dir = std::get<Output>(Step(machine)).val;
std::swap(dx, dy);
if (dir) {
dx = -dx;
} else {
dy = -dy;
}
here.first += dx;
here.second += dy;
}
} }
auto Draw(std::ostream & out, std::map<Coord, ValueType> image) { auto Draw(std::ostream & out, std::map<Coord, ValueType> image) {

7
lib/include/Overload.hpp Normal file
View File

@ -0,0 +1,7 @@
#ifndef OVERLOAD_HPP_
#define OVERLOAD_HPP_
template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
#endif

View File

@ -1,8 +1,10 @@
#ifndef INTCODE_INTERPRETER_HPP_ #ifndef INTCODE_INTERPRETER_HPP_
#define INTCODE_INTERPRETER_HPP_ #define INTCODE_INTERPRETER_HPP_
#include "Machine.hpp" #include <variant>
#include <intcode/Machine.hpp>
#include <Overload.hpp>
namespace intcode { namespace intcode {
struct Input { struct Input {
@ -21,6 +23,21 @@ struct BadInstruction : public std::runtime_error {
explicit BadInstruction(char const* what); explicit BadInstruction(char const* what);
}; };
template <class Fin, class Fout>
auto Run(Machine & machine, Fin input, Fout output) -> void {
while (std::visit(overloaded{
[](Halt) { return false; },
[&](Input arg) {
arg.pos = input();
return true;
},
[&](Output arg) {
output(arg.val);
return true;
}},
Step(machine)));
}
} // namespace } // namespace
#endif #endif

View File

@ -1,11 +1,9 @@
#ifndef INTCODE_HPP_ #ifndef INTCODE_INTCODE_HPP_
#define INTCODE_INTCODE_HPP_ #define INTCODE_INTCODE_HPP_
#include <intcode/Machine.hpp> #include <intcode/Machine.hpp>
#include <intcode/Parser.hpp> #include <intcode/Parser.hpp>
#include <intcode/Interpreter.hpp> #include <intcode/Interpreter.hpp>
#include <Overload.hpp>
template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
#endif #endif