make a common lib for non-intcode problems
This commit is contained in:
2
intcode/CMakeLists.txt
Normal file
2
intcode/CMakeLists.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
add_library(intcode src/Parser.cpp src/Interpreter.cpp src/Machine.cpp)
|
||||
target_include_directories(intcode PUBLIC include)
|
7
intcode/include/Overload.hpp
Normal file
7
intcode/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
|
51
intcode/include/intcode/Interpreter.hpp
Normal file
51
intcode/include/intcode/Interpreter.hpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#ifndef INTCODE_INTERPRETER_HPP_
|
||||
#define INTCODE_INTERPRETER_HPP_
|
||||
|
||||
#include <variant>
|
||||
#include <concepts>
|
||||
|
||||
#include <intcode/Machine.hpp>
|
||||
#include <Overload.hpp>
|
||||
namespace intcode {
|
||||
|
||||
struct Input {
|
||||
ValueType &input;
|
||||
};
|
||||
|
||||
struct Output {
|
||||
ValueType output;
|
||||
};
|
||||
|
||||
struct Halt {};
|
||||
|
||||
auto StepInput(Machine & m, ValueType input) -> void;
|
||||
|
||||
auto StepOutput(Machine & m) -> ValueType;
|
||||
|
||||
auto Step(Machine & m) -> std::variant<Input, Output, Halt>;
|
||||
|
||||
struct BadInstruction : public std::runtime_error {
|
||||
explicit BadInstruction(char const* what);
|
||||
};
|
||||
|
||||
template <std::invocable<ValueType&> Fin, std::invocable<ValueType> Fout, std::invocable<> Fhalt>
|
||||
auto Advance(Machine & machine, Fin input, Fout output, Fhalt halt) {
|
||||
return std::visit(overloaded{
|
||||
[&](Halt) { return halt(); },
|
||||
[&](Input i) { return input(i.input); },
|
||||
[&](Output o) { return output(o.output); },
|
||||
}, Step(machine));
|
||||
}
|
||||
|
||||
template <std::invocable<> Fin, std::invocable<ValueType> Fout>
|
||||
auto Run(Machine & machine, Fin input, Fout output) -> void {
|
||||
while (Advance(machine,
|
||||
[&](ValueType &i) { i = input(); return true; },
|
||||
[&](ValueType o) { output(o); return true; },
|
||||
[]() { return false; }
|
||||
)) {}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif
|
43
intcode/include/intcode/Machine.hpp
Normal file
43
intcode/include/intcode/Machine.hpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifndef INTCODE_MACHINE_HPP_
|
||||
#define INTCODE_MACHINE_HPP_
|
||||
|
||||
#include <cstddef>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
namespace intcode {
|
||||
|
||||
using ValueType = std::int64_t;
|
||||
|
||||
class Machine {
|
||||
std::vector<ValueType> rom_;
|
||||
std::unordered_map<std::size_t, ValueType> ram_;
|
||||
std::size_t pc_;
|
||||
std::size_t base_;
|
||||
|
||||
public:
|
||||
Machine();
|
||||
|
||||
/// Initialize a new machine with a given program starting
|
||||
/// execution and program counter 0.
|
||||
explicit Machine(std::vector<ValueType> program);
|
||||
|
||||
/// Access memory at absolute address
|
||||
/// @param address
|
||||
/// @return reference to memory at given address
|
||||
auto At(std::size_t address) -> ValueType &;
|
||||
|
||||
/// Access memory at address relative to base pointer
|
||||
/// @param offset from base pointer
|
||||
/// @return reference to memory at given offset
|
||||
auto Rel(std::size_t offset) -> ValueType &;
|
||||
|
||||
auto Next() -> ValueType &;
|
||||
auto Goto(std::size_t address) -> void;
|
||||
auto Rebase(std::size_t offset) -> void;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif
|
15
intcode/include/intcode/Parser.hpp
Normal file
15
intcode/include/intcode/Parser.hpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef INTCODE_PARSER_HPP_
|
||||
#define INTCODE_PARSER_HPP_
|
||||
|
||||
#include <istream>
|
||||
#include <vector>
|
||||
|
||||
#include "Machine.hpp"
|
||||
|
||||
namespace intcode {
|
||||
|
||||
auto ParseStream(std::istream &in) -> std::vector<ValueType>;
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif
|
9
intcode/include/intcode/intcode.hpp
Normal file
9
intcode/include/intcode/intcode.hpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef INTCODE_INTCODE_HPP_
|
||||
#define INTCODE_INTCODE_HPP_
|
||||
|
||||
#include <intcode/Interpreter.hpp>
|
||||
#include <intcode/Machine.hpp>
|
||||
#include <intcode/Parser.hpp>
|
||||
#include <Overload.hpp>
|
||||
|
||||
#endif
|
93
intcode/src/Interpreter.cpp
Normal file
93
intcode/src/Interpreter.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
#include <intcode/Interpreter.hpp>
|
||||
|
||||
namespace intcode {
|
||||
|
||||
BadInstruction::BadInstruction(char const* what)
|
||||
: std::runtime_error{what} {}
|
||||
|
||||
auto StepInput(Machine & m, ValueType input) -> void {
|
||||
std::get<Input>(Step(m)).input = input;
|
||||
}
|
||||
|
||||
auto StepOutput(Machine & m) -> ValueType {
|
||||
return std::get<Output>(Step(m)).output;
|
||||
}
|
||||
|
||||
auto Step(Machine & m) -> std::variant<Input, Output, Halt> {
|
||||
for (;;) {
|
||||
auto instruction = m.Next();
|
||||
auto modes = instruction / 10;
|
||||
|
||||
auto arg = [&]() -> ValueType & {
|
||||
auto &v = m.Next();
|
||||
switch ((modes /= 10) % 10) {
|
||||
case 0:
|
||||
return m.At(v);
|
||||
case 1:
|
||||
return v;
|
||||
case 2:
|
||||
return m.Rel(v);
|
||||
default:
|
||||
throw BadInstruction{"invalid addressing mode"};
|
||||
}
|
||||
};
|
||||
|
||||
switch (instruction % 100) {
|
||||
case 1: {
|
||||
arg() = arg() + arg();
|
||||
break;
|
||||
}
|
||||
|
||||
case 2: {
|
||||
arg() = arg() * arg();
|
||||
break;
|
||||
}
|
||||
|
||||
case 3: {
|
||||
return Input{arg()};
|
||||
}
|
||||
|
||||
case 4: {
|
||||
return Output{arg()};
|
||||
}
|
||||
|
||||
case 5: {
|
||||
auto a = arg();
|
||||
auto b = arg();
|
||||
if (a) { m.Goto(b); }
|
||||
break;
|
||||
}
|
||||
|
||||
case 6: {
|
||||
auto a = arg();
|
||||
auto b = arg();
|
||||
if (!a) { m.Goto(b); }
|
||||
break;
|
||||
}
|
||||
|
||||
case 7: {
|
||||
auto a = arg();
|
||||
auto b = arg();
|
||||
arg() = a < b; // order matters
|
||||
break;
|
||||
}
|
||||
|
||||
case 8: {
|
||||
arg() = arg() == arg();
|
||||
break;
|
||||
}
|
||||
|
||||
case 9:
|
||||
m.Rebase(arg());
|
||||
break;
|
||||
|
||||
case 99:
|
||||
return Halt{};
|
||||
|
||||
default:
|
||||
throw BadInstruction{"invalid opcode"};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
30
intcode/src/Machine.cpp
Normal file
30
intcode/src/Machine.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <intcode/Machine.hpp>
|
||||
|
||||
namespace intcode {
|
||||
|
||||
Machine::Machine() : rom_{}, ram_{}, pc_{0}, base_{0} {}
|
||||
|
||||
Machine::Machine(std::vector<ValueType> program)
|
||||
: rom_{program}, ram_{}, pc_{0}, base_{0} {}
|
||||
|
||||
auto Machine::At(std::size_t i) -> ValueType & {
|
||||
return i < rom_.size() ? rom_[i] : ram_[i];
|
||||
}
|
||||
|
||||
auto Machine::Rel(std::size_t i) -> ValueType & {
|
||||
return At(base_ + i);
|
||||
}
|
||||
|
||||
auto Machine::Rebase(std::size_t offset) -> void {
|
||||
base_ += offset;
|
||||
}
|
||||
|
||||
auto Machine::Next() -> ValueType & {
|
||||
return At(pc_++);
|
||||
}
|
||||
|
||||
auto Machine::Goto(std::size_t address) -> void {
|
||||
pc_ = address;
|
||||
}
|
||||
|
||||
} // namespace
|
17
intcode/src/Parser.cpp
Normal file
17
intcode/src/Parser.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <intcode/Parser.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace intcode {
|
||||
|
||||
auto ParseStream(std::istream &in) -> std::vector<ValueType> {
|
||||
ValueType x;
|
||||
std::string str;
|
||||
std::vector<ValueType> result;
|
||||
while (std::getline(in, str, ',')) {
|
||||
result.push_back(std::stol(str));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
Reference in New Issue
Block a user