aocpp/lib/include/intcode.hpp

57 lines
1.1 KiB
C++
Raw Normal View History

2022-11-03 08:15:17 -07:00
#ifndef INTCODE_HPP_
#define INTCODE_HPP_
#include <cstddef>
#include <cstdint>
2022-11-03 12:53:10 -07:00
#include <istream>
2022-11-03 08:15:17 -07:00
#include <unordered_map>
2022-11-03 12:53:10 -07:00
#include <variant>
#include <vector>
2022-11-03 08:15:17 -07:00
2022-11-03 12:53:10 -07:00
template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
2022-11-03 08:15:17 -07:00
2022-11-03 12:53:10 -07:00
namespace intcode {
2022-11-03 08:15:17 -07:00
2022-11-03 12:53:10 -07:00
using value_type = std::int64_t;
2022-11-03 08:15:17 -07:00
2022-11-03 12:53:10 -07:00
struct Input {
value_type &pos;
};
2022-11-03 08:15:17 -07:00
2022-11-03 12:53:10 -07:00
struct Output {
value_type val;
};
2022-11-03 08:15:17 -07:00
2022-11-03 12:53:10 -07:00
struct Halt {};
2022-11-03 08:15:17 -07:00
2022-11-03 12:53:10 -07:00
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 &;
2022-11-03 08:15:17 -07:00
public:
2022-11-03 12:53:10 -07:00
Machine();
explicit Machine(std::vector<value_type> ram);
auto step() -> std::variant<Input, Output, Halt>;
auto at(std::size_t) -> value_type &;
2022-11-03 08:15:17 -07:00
};
2022-11-03 12:53:10 -07:00
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>;
}
2022-11-03 08:15:17 -07:00
2022-11-03 12:53:10 -07:00
#endif // INTCODE_HPP_