This commit is contained in:
2022-11-03 08:15:17 -07:00
commit 1936f63d47
9 changed files with 320 additions and 0 deletions

2
lib/CMakeLists.txt Normal file
View File

@@ -0,0 +1,2 @@
add_library(intcode intcode.cpp)
target_include_directories(intcode PUBLIC include)

49
lib/include/intcode.hpp Normal file
View File

@@ -0,0 +1,49 @@
#ifndef INTCODE_HPP_
#define INTCODE_HPP_
#include <istream>
#include <cstddef>
#include <vector>
#include <cstdint>
#include <variant>
#include <unordered_map>
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
struct BadInstruction : public std::exception {
std::int64_t instruction;
explicit BadInstruction(std::int64_t instruction) : instruction{instruction} {}
const char* what() const noexcept override {
return "bad instruction";
}
};
struct Input {
std::int64_t& pos;
};
struct Output {
std::int64_t val;
};
struct Halt {};
class intcode {
std::vector<std::int64_t> rom_;
std::unordered_map<std::size_t, std::int64_t> ram_;
std::size_t pc_;
std::size_t base_;
auto ref(std::int64_t instruction, std::int64_t p, std::size_t offset) -> std::int64_t&;
public:
intcode();
intcode(std::vector<std::int64_t> ram);
auto step() -> std::variant<Input, Output, Halt>;
auto at(std::size_t) -> std::int64_t&;
};
auto parse_stream(std::istream& in) -> std::vector<std::int64_t>;
#endif // INTCODE_HPP_

92
lib/intcode.cpp Normal file
View File

@@ -0,0 +1,92 @@
#include "intcode.hpp"
#include <iostream>
intcode::intcode() : rom_{}, ram_{}, pc_{0}, base_{0} {}
intcode::intcode(std::vector<std::int64_t> ram) : rom_{ram}, ram_{}, pc_{0}, base_{0} {}
auto intcode::ref(std::int64_t instruction, std::int64_t p, std::size_t offset) -> std::int64_t& {
auto& i = at(pc_ + offset);
switch (instruction / p % 10) {
case 0: return at(i);
case 1: return i;
case 2: return at(base_ + i);
default: throw BadInstruction{instruction};
}
}
auto intcode::at(std::size_t i) -> std::int64_t& {
return i < rom_.size() ? rom_[i] : ram_[i];
}
auto intcode::step() -> std::variant<Input, Output, Halt> {
for (;;) {
auto instruction = at(pc_);
auto a = [this,instruction]() -> std::int64_t& { return ref(instruction, 100, 1); };
auto b = [this,instruction]() -> std::int64_t& { return ref(instruction, 1000, 2); };
auto c = [this,instruction]() -> std::int64_t& { return ref(instruction, 10000, 3); };
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:
throw BadInstruction{instruction};
}
}
}
auto parse_stream(std::istream& in) -> std::vector<std::int64_t> {
std::int64_t x;
std::string str;
std::vector<std::int64_t> result;
while(std::getline(in, str, ',')) {
result.push_back(std::stoi(str));
}
return result;
}