62 lines
1.3 KiB
C++
62 lines
1.3 KiB
C++
#ifndef INTCODE_HPP_
|
|
#define INTCODE_HPP_
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <istream>
|
|
#include <unordered_map>
|
|
#include <stdexcept>
|
|
#include <variant>
|
|
#include <vector>
|
|
|
|
template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
|
|
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
|
|
|
|
namespace intcode {
|
|
|
|
using ValueType = std::int64_t;
|
|
|
|
struct Input {
|
|
ValueType &pos;
|
|
};
|
|
|
|
struct Output {
|
|
ValueType val;
|
|
};
|
|
|
|
struct Halt {};
|
|
|
|
class Machine {
|
|
std::vector<ValueType> rom_;
|
|
std::unordered_map<std::size_t, ValueType> ram_;
|
|
std::size_t pc_;
|
|
std::size_t base_;
|
|
|
|
public:
|
|
Machine();
|
|
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::runtime_error {
|
|
explicit BadInstruction(char const* what);
|
|
};
|
|
|
|
auto ParseStream(std::istream &in) -> std::vector<ValueType>;
|
|
|
|
}
|
|
|
|
#endif // INTCODE_HPP_
|