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
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-11-04 08:29:02 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Step(Machine & m) -> std::variant<Input, Output, Halt> {
|
2022-11-03 12:53:10 -07:00
|
|
|
for (;;) {
|
2022-11-04 08:29:02 -07:00
|
|
|
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"};
|
|
|
|
}
|
|
|
|
};
|
2022-11-03 12:53:10 -07:00
|
|
|
|
|
|
|
switch (instruction % 100) {
|
2022-11-04 08:29:02 -07:00
|
|
|
case 1: {
|
|
|
|
auto a = arg();
|
|
|
|
auto b = arg();
|
|
|
|
auto& c = arg();
|
|
|
|
c = a + b;
|
2022-11-03 12:53:10 -07:00
|
|
|
break;
|
2022-11-04 08:29:02 -07:00
|
|
|
}
|
2022-11-03 12:53:10 -07:00
|
|
|
|
2022-11-04 08:29:02 -07:00
|
|
|
case 2: {
|
|
|
|
auto a = arg();
|
|
|
|
auto b = arg();
|
|
|
|
auto& c = arg();
|
|
|
|
c = a * b;
|
2022-11-03 12:53:10 -07:00
|
|
|
break;
|
2022-11-04 08:29:02 -07:00
|
|
|
}
|
2022-11-03 12:53:10 -07:00
|
|
|
|
|
|
|
case 3: {
|
2022-11-04 08:29:02 -07:00
|
|
|
return Input{arg()};
|
2022-11-03 12:53:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
case 4: {
|
2022-11-04 08:29:02 -07:00
|
|
|
return Output{arg()};
|
2022-11-03 12:53:10 -07:00
|
|
|
}
|
|
|
|
|
2022-11-04 08:29:02 -07:00
|
|
|
case 5: {
|
|
|
|
auto a = arg();
|
|
|
|
auto b = arg();
|
|
|
|
if (a) { m.Goto(b); }
|
2022-11-03 12:53:10 -07:00
|
|
|
break;
|
2022-11-04 08:29:02 -07:00
|
|
|
}
|
2022-11-03 12:53:10 -07:00
|
|
|
|
2022-11-04 08:29:02 -07:00
|
|
|
case 6: {
|
|
|
|
auto a = arg();
|
|
|
|
auto b = arg();
|
|
|
|
if (!a) { m.Goto(b); }
|
2022-11-03 12:53:10 -07:00
|
|
|
break;
|
2022-11-04 08:29:02 -07:00
|
|
|
}
|
2022-11-03 12:53:10 -07:00
|
|
|
|
2022-11-04 08:29:02 -07:00
|
|
|
case 7: {
|
|
|
|
auto a = arg();
|
|
|
|
auto b = arg();
|
|
|
|
auto& c = arg();
|
|
|
|
c = a < b;
|
2022-11-03 12:53:10 -07:00
|
|
|
break;
|
2022-11-04 08:29:02 -07:00
|
|
|
}
|
2022-11-03 12:53:10 -07:00
|
|
|
|
2022-11-04 08:29:02 -07:00
|
|
|
case 8: {
|
|
|
|
auto a = arg();
|
|
|
|
auto b = arg();
|
|
|
|
auto& c = arg();
|
|
|
|
c = a == b;
|
2022-11-03 12:53:10 -07:00
|
|
|
break;
|
2022-11-04 08:29:02 -07:00
|
|
|
}
|
2022-11-03 12:53:10 -07:00
|
|
|
|
|
|
|
case 9:
|
2022-11-04 08:29:02 -07:00
|
|
|
m.Rebase(arg());
|
2022-11-03 12:53:10 -07:00
|
|
|
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
|
|
|
}
|