2022-11-03 08:15:17 -07:00
|
|
|
#include "intcode.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
|
2022-11-03 12:53:10 -07:00
|
|
|
namespace intcode {
|
|
|
|
|
2022-11-03 14:43:47 -07:00
|
|
|
BadInstruction::BadInstruction(char const* what)
|
|
|
|
: std::runtime_error{what} {}
|
2022-11-03 12:53:10 -07:00
|
|
|
|
|
|
|
Machine::Machine() : rom_{}, ram_{}, pc_{0}, base_{0} {}
|
|
|
|
|
2022-11-03 14:43:47 -07:00
|
|
|
Machine::Machine(std::vector<ValueType> ram)
|
2022-11-03 12:53:10 -07:00
|
|
|
: rom_{ram}, ram_{}, pc_{0}, base_{0} {}
|
|
|
|
|
2022-11-03 14:43:47 -07:00
|
|
|
namespace {
|
|
|
|
auto Ref(Machine & m, ValueType instruction, std::size_t k, ValueType p)
|
|
|
|
-> ValueType & {
|
|
|
|
auto &v = m.At(k);
|
2022-11-03 12:53:10 -07:00
|
|
|
switch (instruction / p % 10) {
|
|
|
|
case 0:
|
2022-11-03 14:43:47 -07:00
|
|
|
return m.At(v);
|
2022-11-03 12:53:10 -07:00
|
|
|
case 1:
|
2022-11-03 14:43:47 -07:00
|
|
|
return v;
|
2022-11-03 12:53:10 -07:00
|
|
|
case 2:
|
2022-11-03 20:16:11 -07:00
|
|
|
return m.Rel(v);
|
2022-11-03 12:53:10 -07:00
|
|
|
default:
|
2022-11-03 14:43:47 -07:00
|
|
|
throw BadInstruction{"invalid addressing mode"};
|
2022-11-03 12:53:10 -07:00
|
|
|
}
|
2022-11-03 08:15:17 -07:00
|
|
|
}
|
2022-11-03 14:43:47 -07:00
|
|
|
}
|
2022-11-03 08:15:17 -07:00
|
|
|
|
2022-11-03 14:43:47 -07:00
|
|
|
auto Machine::At(std::size_t i) -> ValueType & {
|
2022-11-03 12:53:10 -07:00
|
|
|
return i < rom_.size() ? rom_[i] : ram_[i];
|
2022-11-03 08:15:17 -07:00
|
|
|
}
|
|
|
|
|
2022-11-03 14:43:47 -07:00
|
|
|
auto Machine::Rel(std::size_t i) -> ValueType & {
|
|
|
|
return At(base_ + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Machine::Step() -> std::variant<Input, Output, Halt> {
|
2022-11-03 12:53:10 -07:00
|
|
|
for (;;) {
|
2022-11-03 14:43:47 -07:00
|
|
|
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); };
|
2022-11-03 12:53:10 -07:00
|
|
|
|
|
|
|
switch (instruction % 100) {
|
|
|
|
case 1:
|
|
|
|
c() = a() + b();
|
|
|
|
pc_ += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
c() = a() * b();
|
|
|
|
pc_ += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: {
|
|
|
|
auto &pos = a();
|
|
|
|
pc_ += 2;
|
|
|
|
return Input{pos};
|
|
|
|
}
|
|
|
|
|
|
|
|
case 4: {
|
|
|
|
auto val = a();
|
|
|
|
pc_ += 2;
|
|
|
|
return Output{val};
|
|
|
|
}
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
pc_ = a() ? b() : pc_ + 3;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
pc_ = a() ? pc_ + 3 : b();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 7:
|
|
|
|
c() = a() < b();
|
|
|
|
pc_ += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
c() = a() == b();
|
|
|
|
pc_ += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 9:
|
|
|
|
base_ += a();
|
|
|
|
pc_ += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 99:
|
|
|
|
return Halt{};
|
|
|
|
|
|
|
|
default:
|
2022-11-03 14:43:47 -07:00
|
|
|
throw BadInstruction{"invalid opcode"};
|
2022-11-03 12:53:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 14:43:47 -07:00
|
|
|
auto ParseStream(std::istream &in) -> std::vector<ValueType> {
|
|
|
|
ValueType x;
|
2022-11-03 12:53:10 -07:00
|
|
|
std::string str;
|
2022-11-03 14:43:47 -07:00
|
|
|
std::vector<ValueType> result;
|
2022-11-03 12:53:10 -07:00
|
|
|
while (std::getline(in, str, ',')) {
|
2022-11-03 19:49:07 -07:00
|
|
|
result.push_back(std::stol(str));
|
2022-11-03 12:53:10 -07:00
|
|
|
}
|
|
|
|
return result;
|
2022-11-03 08:15:17 -07:00
|
|
|
}
|
|
|
|
|
2022-11-03 12:53:10 -07:00
|
|
|
}
|