Run
This commit is contained in:
parent
e98a71dad6
commit
8d65f74d9f
14
day05.cpp
14
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;
|
||||
}
|
||||
|
||||
|
14
day09.cpp
14
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;
|
||||
}
|
||||
|
||||
|
33
day11.cpp
33
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<Halt>(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<Input>(effect).pos = paint[here];
|
||||
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;
|
||||
}
|
||||
return paint;
|
||||
}
|
||||
|
||||
auto Draw(std::ostream & out, std::map<Coord, ValueType> image) {
|
||||
|
7
lib/include/Overload.hpp
Normal file
7
lib/include/Overload.hpp
Normal 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
|
@ -1,8 +1,10 @@
|
||||
#ifndef INTCODE_INTERPRETER_HPP_
|
||||
#define INTCODE_INTERPRETER_HPP_
|
||||
|
||||
#include "Machine.hpp"
|
||||
#include <variant>
|
||||
|
||||
#include <intcode/Machine.hpp>
|
||||
#include <Overload.hpp>
|
||||
namespace intcode {
|
||||
|
||||
struct Input {
|
||||
@ -21,6 +23,21 @@ struct BadInstruction : public std::runtime_error {
|
||||
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
|
||||
|
||||
#endif
|
||||
|
@ -1,11 +1,9 @@
|
||||
#ifndef INTCODE_HPP_
|
||||
#ifndef INTCODE_INTCODE_HPP_
|
||||
#define INTCODE_INTCODE_HPP_
|
||||
|
||||
#include <intcode/Machine.hpp>
|
||||
#include <intcode/Parser.hpp>
|
||||
#include <intcode/Interpreter.hpp>
|
||||
|
||||
template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
|
||||
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
|
||||
#include <Overload.hpp>
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user