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>;
}