This commit is contained in:
Eric Mertens
2022-11-03 14:43:47 -07:00
parent 40f58503dc
commit 898ae0521b
6 changed files with 83 additions and 74 deletions

View File

@@ -5,6 +5,7 @@
#include <cstdint>
#include <istream>
#include <unordered_map>
#include <stdexcept>
#include <variant>
#include <vector>
@@ -13,43 +14,47 @@ template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
namespace intcode {
using value_type = std::int64_t;
using ValueType = std::int64_t;
struct Input {
value_type &pos;
ValueType &pos;
};
struct Output {
value_type val;
ValueType val;
};
struct Halt {};
class Machine {
std::vector<value_type> rom_;
std::unordered_map<std::size_t, value_type> ram_;
std::vector<ValueType> rom_;
std::unordered_map<std::size_t, ValueType> ram_;
std::size_t pc_;
std::size_t base_;
auto ref(value_type instruction, value_type p, std::size_t offset)
-> value_type &;
public:
Machine();
explicit Machine(std::vector<value_type> ram);
auto step() -> std::variant<Input, Output, Halt>;
auto at(std::size_t) -> value_type &;
explicit Machine(std::vector<ValueType> ram);
/// Advance machine until next side effect
auto Step() -> std::variant<Input, Output, Halt>;
/// 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 &;
};
struct BadInstruction : public std::exception {
std::size_t pc;
value_type instruction;
explicit BadInstruction(std::size_t pc, value_type instruction);
const char *what() const noexcept override;
struct BadInstruction : public std::runtime_error {
explicit BadInstruction(char const* what);
};
auto parse_stream(std::istream &in) -> std::vector<value_type>;
auto ParseStream(std::istream &in) -> std::vector<ValueType>;
}

View File

@@ -3,41 +3,45 @@
namespace intcode {
BadInstruction::BadInstruction(std::size_t pc, value_type instruction)
: pc{pc}, instruction{instruction} {}
char const *BadInstruction::what() const noexcept { return "bad instruction"; }
BadInstruction::BadInstruction(char const* what)
: std::runtime_error{what} {}
Machine::Machine() : rom_{}, ram_{}, pc_{0}, base_{0} {}
Machine::Machine(std::vector<value_type> ram)
Machine::Machine(std::vector<ValueType> ram)
: rom_{ram}, ram_{}, pc_{0}, base_{0} {}
auto Machine::ref(value_type instruction, value_type p, std::size_t offset)
-> value_type & {
auto &i = at(pc_ + offset);
namespace {
auto Ref(Machine & m, ValueType instruction, std::size_t k, ValueType p)
-> ValueType & {
auto &v = m.At(k);
switch (instruction / p % 10) {
case 0:
return at(i);
return m.At(v);
case 1:
return i;
return v;
case 2:
return at(base_ + i);
return m.At(m.Rel(v));
default:
throw BadInstruction{pc_, instruction};
throw BadInstruction{"invalid addressing mode"};
}
}
}
auto Machine::at(std::size_t i) -> value_type & {
auto Machine::At(std::size_t i) -> ValueType & {
return i < rom_.size() ? rom_[i] : ram_[i];
}
auto Machine::step() -> std::variant<Input, Output, Halt> {
auto Machine::Rel(std::size_t i) -> ValueType & {
return At(base_ + i);
}
auto Machine::Step() -> std::variant<Input, Output, Halt> {
for (;;) {
auto instruction = at(pc_);
auto a = [=]() -> auto & { return ref(instruction, 100, 1); };
auto b = [=]() -> auto & { return ref(instruction, 1000, 2); };
auto c = [=]() -> auto & { return ref(instruction, 10000, 3); };
auto instruction = At(pc_);
auto a = [=]() -> auto & { return Ref(*this, instruction, pc_+1, 100); };
auto b = [=]() -> auto & { return Ref(*this, instruction, pc_+2, 1000); };
auto c = [=]() -> auto & { return Ref(*this, instruction, pc_+3, 10000); };
switch (instruction % 100) {
case 1:
@@ -89,15 +93,15 @@ auto Machine::step() -> std::variant<Input, Output, Halt> {
return Halt{};
default:
throw BadInstruction{pc_, instruction};
throw BadInstruction{"invalid opcode"};
}
}
}
auto parse_stream(std::istream &in) -> std::vector<value_type> {
value_type x;
auto ParseStream(std::istream &in) -> std::vector<ValueType> {
ValueType x;
std::string str;
std::vector<value_type> result;
std::vector<ValueType> result;
while (std::getline(in, str, ',')) {
result.push_back(std::stoi(str));
}