aocpp/lib/include/intcode.hpp
2022-11-03 12:53:10 -07:00

57 lines
1.1 KiB
C++

#ifndef INTCODE_HPP_
#define INTCODE_HPP_
#include <cstddef>
#include <cstdint>
#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...>;
namespace intcode {
using value_type = std::int64_t;
struct Input {
value_type &pos;
};
struct Output {
value_type val;
};
struct Halt {};
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:
Machine();
explicit Machine(std::vector<value_type> ram);
auto step() -> std::variant<Input, Output, Halt>;
auto at(std::size_t) -> value_type &;
};
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;
};
auto parse_stream(std::istream &in) -> std::vector<value_type>;
}
#endif // INTCODE_HPP_