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

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_
#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

View File

@@ -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