diff --git a/day05.cpp b/day05.cpp index 92f1dc4..0929a21 100644 --- a/day05.cpp +++ b/day05.cpp @@ -8,17 +8,9 @@ namespace { auto Compute(Machine machine, ValueType d) -> ValueType { ValueType last_output = -1; - while (std::visit(overloaded{ - [](Halt) { return false; }, - [d, &machine](Input arg) { - arg.pos = d; - return true; - }, - [&last_output](Output arg) { - last_output = arg.val; - return true; - }}, - Step(machine))); + Run(machine, + [=]() { return d; }, + [&](auto o) { last_output = o; }); return last_output; } diff --git a/day09.cpp b/day09.cpp index 0779504..844e262 100644 --- a/day09.cpp +++ b/day09.cpp @@ -8,17 +8,9 @@ namespace { auto Compute(Machine machine, ValueType d) -> ValueType { ValueType output = -1; - while (std::visit(overloaded{ - [](Halt) { return false; }, - [d](Input arg) { - arg.pos = d; - return true; - }, - [&output](Output arg) { - output = arg.val; - return false; - }}, - Step(machine))); + Run(machine, + [=](){ return d; }, + [&](auto o) { output = o; }); return output; } diff --git a/day11.cpp b/day11.cpp index 7f98690..9caf0f2 100644 --- a/day11.cpp +++ b/day11.cpp @@ -18,26 +18,23 @@ auto Compute(Machine machine, ValueType start) ValueType dx {0}; ValueType dy {-1}; - for (;;) { - auto effect = Step(machine); + bool next_color = true; - if (std::holds_alternative(effect)) { - return paint; - } + Run(machine, + [&]() { 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(effect).pos = paint[here]; - paint[here] = std::get(Step(machine)).val; - auto dir = std::get(Step(machine)).val; - - std::swap(dx, dy); - if (dir) { - dx = -dx; - } else { - dy = -dy; - } - here.first += dx; - here.second += dy; - } + return paint; } auto Draw(std::ostream & out, std::map image) { diff --git a/lib/include/Overload.hpp b/lib/include/Overload.hpp new file mode 100644 index 0000000..9dcdaae --- /dev/null +++ b/lib/include/Overload.hpp @@ -0,0 +1,7 @@ +#ifndef OVERLOAD_HPP_ +#define OVERLOAD_HPP_ + +template struct overloaded : Ts... { using Ts::operator()...; }; +template overloaded(Ts...) -> overloaded; + +#endif diff --git a/lib/include/intcode/Interpreter.hpp b/lib/include/intcode/Interpreter.hpp index bb8967a..e3ca67a 100644 --- a/lib/include/intcode/Interpreter.hpp +++ b/lib/include/intcode/Interpreter.hpp @@ -1,8 +1,10 @@ #ifndef INTCODE_INTERPRETER_HPP_ #define INTCODE_INTERPRETER_HPP_ -#include "Machine.hpp" +#include +#include +#include namespace intcode { struct Input { @@ -21,6 +23,21 @@ struct BadInstruction : public std::runtime_error { explicit BadInstruction(char const* what); }; +template +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 #endif diff --git a/lib/include/intcode/intcode.hpp b/lib/include/intcode/intcode.hpp index a56803b..7506259 100644 --- a/lib/include/intcode/intcode.hpp +++ b/lib/include/intcode/intcode.hpp @@ -1,11 +1,9 @@ -#ifndef INTCODE_HPP_ +#ifndef INTCODE_INTCODE_HPP_ #define INTCODE_INTCODE_HPP_ #include #include #include - -template struct overloaded : Ts... { using Ts::operator()...; }; -template overloaded(Ts...) -> overloaded; +#include #endif