checkpoint

This commit is contained in:
Eric Mertens
2022-11-03 12:53:10 -07:00
parent 1936f63d47
commit 40f58503dc
7 changed files with 238 additions and 211 deletions

View File

@@ -1,49 +1,56 @@
#ifndef INTCODE_HPP_
#define INTCODE_HPP_
#include <istream>
#include <cstddef>
#include <vector>
#include <cstdint>
#include <variant>
#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...>;
template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
struct BadInstruction : public std::exception {
std::int64_t instruction;
explicit BadInstruction(std::int64_t instruction) : instruction{instruction} {}
const char* what() const noexcept override {
return "bad instruction";
}
};
namespace intcode {
struct Input {
std::int64_t& pos;
};
using value_type = std::int64_t;
struct Output {
std::int64_t val;
};
struct Input {
value_type &pos;
};
struct Halt {};
struct Output {
value_type val;
};
class intcode {
std::vector<std::int64_t> rom_;
std::unordered_map<std::size_t, std::int64_t> ram_;
std::size_t pc_;
std::size_t base_;
struct Halt {};
auto ref(std::int64_t instruction, std::int64_t p, std::size_t offset) -> std::int64_t&;
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:
intcode();
intcode(std::vector<std::int64_t> ram);
auto step() -> std::variant<Input, Output, Halt>;
auto at(std::size_t) -> std::int64_t&;
Machine();
explicit Machine(std::vector<value_type> ram);
auto step() -> std::variant<Input, Output, Halt>;
auto at(std::size_t) -> value_type &;
};
auto parse_stream(std::istream& in) -> std::vector<std::int64_t>;
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;
};
#endif // INTCODE_HPP_
auto parse_stream(std::istream &in) -> std::vector<value_type>;
}
#endif // INTCODE_HPP_