#ifndef INTCODE_HPP_ #define INTCODE_HPP_ #include #include #include #include #include #include #include template struct overloaded : Ts... { using Ts::operator()...; }; template overloaded(Ts...) -> overloaded; namespace intcode { using ValueType = std::int64_t; struct Input { ValueType &pos; }; struct Output { ValueType val; }; struct Halt {}; class Machine { std::vector rom_; std::unordered_map ram_; std::size_t pc_; std::size_t base_; public: Machine(); explicit Machine(std::vector ram); /// 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 &; auto Next() -> ValueType &; auto Goto(std::size_t address) -> void; auto Rebase(std::size_t offset) -> void; }; auto Step(Machine & m) -> std::variant; struct BadInstruction : public std::runtime_error { explicit BadInstruction(char const* what); }; auto ParseStream(std::istream &in) -> std::vector; } #endif // INTCODE_HPP_