checkpoint
This commit is contained in:
@@ -1,49 +1,56 @@
|
||||
#ifndef INTCODE_HPP_
|
||||
#define INTCODE_HPP_
|
||||
|
||||
#include <istream>
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
#include <variant>
|
||||
#include <istream>
|
||||
#include <unordered_map>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
|
||||
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
|
||||
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";
|
||||
}
|
||||
};
|
||||
namespace intcode {
|
||||
|
||||
struct Input {
|
||||
std::int64_t& pos;
|
||||
};
|
||||
using value_type = std::int64_t;
|
||||
|
||||
struct Output {
|
||||
std::int64_t val;
|
||||
};
|
||||
struct Input {
|
||||
value_type &pos;
|
||||
};
|
||||
|
||||
struct Halt {};
|
||||
struct Output {
|
||||
value_type val;
|
||||
};
|
||||
|
||||
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_;
|
||||
struct Halt {};
|
||||
|
||||
auto ref(std::int64_t instruction, std::int64_t p, std::size_t offset) -> std::int64_t&;
|
||||
class Machine {
|
||||
std::vector<value_type> rom_;
|
||||
std::unordered_map<std::size_t, value_type> ram_;
|
||||
std::size_t pc_;
|
||||
std::size_t base_;
|
||||
|
||||
auto ref(value_type instruction, value_type p, std::size_t offset)
|
||||
-> value_type &;
|
||||
|
||||
public:
|
||||
intcode();
|
||||
intcode(std::vector<std::int64_t> ram);
|
||||
auto step() -> std::variant<Input, Output, Halt>;
|
||||
auto at(std::size_t) -> std::int64_t&;
|
||||
Machine();
|
||||
explicit Machine(std::vector<value_type> ram);
|
||||
auto step() -> std::variant<Input, Output, Halt>;
|
||||
auto at(std::size_t) -> value_type &;
|
||||
|
||||
};
|
||||
|
||||
auto parse_stream(std::istream& in) -> std::vector<std::int64_t>;
|
||||
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;
|
||||
};
|
||||
|
||||
#endif // INTCODE_HPP_
|
||||
auto parse_stream(std::istream &in) -> std::vector<value_type>;
|
||||
|
||||
}
|
||||
|
||||
#endif // INTCODE_HPP_
|
||||
|
163
lib/intcode.cpp
163
lib/intcode.cpp
@@ -1,92 +1,107 @@
|
||||
#include "intcode.hpp"
|
||||
#include <iostream>
|
||||
|
||||
intcode::intcode() : rom_{}, ram_{}, pc_{0}, base_{0} {}
|
||||
namespace intcode {
|
||||
|
||||
intcode::intcode(std::vector<std::int64_t> ram) : rom_{ram}, ram_{}, pc_{0}, base_{0} {}
|
||||
BadInstruction::BadInstruction(std::size_t pc, value_type instruction)
|
||||
: pc{pc}, instruction{instruction} {}
|
||||
|
||||
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};
|
||||
}
|
||||
char const *BadInstruction::what() const noexcept { return "bad instruction"; }
|
||||
|
||||
Machine::Machine() : rom_{}, ram_{}, pc_{0}, base_{0} {}
|
||||
|
||||
Machine::Machine(std::vector<value_type> 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);
|
||||
switch (instruction / p % 10) {
|
||||
case 0:
|
||||
return at(i);
|
||||
case 1:
|
||||
return i;
|
||||
case 2:
|
||||
return at(base_ + i);
|
||||
default:
|
||||
throw BadInstruction{pc_, instruction};
|
||||
}
|
||||
}
|
||||
|
||||
auto intcode::at(std::size_t i) -> std::int64_t& {
|
||||
return i < rom_.size() ? rom_[i] : ram_[i];
|
||||
auto Machine::at(std::size_t i) -> value_type & {
|
||||
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); };
|
||||
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); };
|
||||
|
||||
switch (instruction % 100) {
|
||||
case 1:
|
||||
c() = a() + b();
|
||||
pc_ += 4;
|
||||
break;
|
||||
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 2:
|
||||
c() = a() * b();
|
||||
pc_ += 4;
|
||||
break;
|
||||
|
||||
case 4: {
|
||||
auto val = a();
|
||||
pc_ += 2;
|
||||
return Output{val};
|
||||
}
|
||||
case 3: {
|
||||
auto &pos = a();
|
||||
pc_ += 2;
|
||||
return Input{pos};
|
||||
}
|
||||
|
||||
case 5:
|
||||
pc_ = a() ? b() : pc_ + 3;
|
||||
break;
|
||||
case 4: {
|
||||
auto val = a();
|
||||
pc_ += 2;
|
||||
return Output{val};
|
||||
}
|
||||
|
||||
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 5:
|
||||
pc_ = a() ? b() : pc_ + 3;
|
||||
break;
|
||||
|
||||
case 99:
|
||||
return Halt{};
|
||||
case 6:
|
||||
pc_ = a() ? pc_ + 3 : b();
|
||||
break;
|
||||
|
||||
default:
|
||||
throw BadInstruction{instruction};
|
||||
}
|
||||
}
|
||||
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{pc_, 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;
|
||||
}
|
||||
auto parse_stream(std::istream &in) -> std::vector<value_type> {
|
||||
value_type x;
|
||||
std::string str;
|
||||
std::vector<value_type> result;
|
||||
while (std::getline(in, str, ',')) {
|
||||
result.push_back(std::stoi(str));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user