make a common lib for non-intcode problems

This commit is contained in:
2022-11-06 21:12:30 -08:00
parent 041c055f43
commit ebb068cfc5
24 changed files with 131 additions and 42 deletions

View File

@@ -1,7 +0,0 @@
#ifndef OVERLOAD_HPP_
#define OVERLOAD_HPP_
template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
#endif

View File

@@ -1,14 +1,12 @@
#ifndef INTCODE_COORD_HPP_
#define INTCODE_COORD_HPP_
#ifndef AOCPP_COORD_HPP_
#define AOCPP_COORD_HPP_
#include <functional>
#include <map>
#include <ostream>
#include <tuple>
#include <intcode/Machine.hpp>
namespace intcode {
namespace aocpp {
/// Cartesian coordinate
///
@@ -19,7 +17,7 @@ struct Coord {
};
auto Draw(std::ostream & out, std::map<Coord, ValueType> image) -> void;
auto Draw(std::ostream & out, std::map<Coord, std::int64_t> image) -> void;
/// Move one unit up
auto Up(Coord) -> Coord;
@@ -60,9 +58,9 @@ auto operator<<(std::ostream &, Coord) -> std::ostream &;
} // namespace
template<>
struct std::hash<intcode::Coord>
struct std::hash<aocpp::Coord>
{
auto operator()(intcode::Coord const& c) const noexcept -> std::size_t {
auto operator()(aocpp::Coord const& c) const noexcept -> std::size_t {
std::hash<std::int64_t> h;
return h(c.x) ^ h(c.y);
}

View File

@@ -0,0 +1,10 @@
#ifndef AOCPP_STARTUP_HPP_
#define AOCPP_STARTUP_HPP_
#include <fstream>
namespace aocpp {
auto Startup(int argc, char ** argv) -> std::ifstream;
}
#endif

View File

@@ -1,51 +0,0 @@
#ifndef INTCODE_INTERPRETER_HPP_
#define INTCODE_INTERPRETER_HPP_
#include <variant>
#include <concepts>
#include <intcode/Machine.hpp>
#include <Overload.hpp>
namespace intcode {
struct Input {
ValueType &input;
};
struct Output {
ValueType output;
};
struct Halt {};
auto StepInput(Machine & m, ValueType input) -> void;
auto StepOutput(Machine & m) -> ValueType;
auto Step(Machine & m) -> std::variant<Input, Output, Halt>;
struct BadInstruction : public std::runtime_error {
explicit BadInstruction(char const* what);
};
template <std::invocable<ValueType&> Fin, std::invocable<ValueType> Fout, std::invocable<> Fhalt>
auto Advance(Machine & machine, Fin input, Fout output, Fhalt halt) {
return std::visit(overloaded{
[&](Halt) { return halt(); },
[&](Input i) { return input(i.input); },
[&](Output o) { return output(o.output); },
}, Step(machine));
}
template <std::invocable<> Fin, std::invocable<ValueType> Fout>
auto Run(Machine & machine, Fin input, Fout output) -> void {
while (Advance(machine,
[&](ValueType &i) { i = input(); return true; },
[&](ValueType o) { output(o); return true; },
[]() { return false; }
)) {}
}
} // namespace
#endif

View File

@@ -1,39 +0,0 @@
#ifndef INTCODE_MACHINE_HPP_
#define INTCODE_MACHINE_HPP_
#include <cstddef>
#include <unordered_map>
#include <vector>
namespace intcode {
using ValueType = std::int64_t;
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);
/// 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;
};
} // namespace
#endif

View File

@@ -1,15 +0,0 @@
#ifndef INTCODE_PARSER_HPP_
#define INTCODE_PARSER_HPP_
#include <istream>
#include <vector>
#include "Machine.hpp"
namespace intcode {
auto ParseStream(std::istream &in) -> std::vector<ValueType>;
} // namespace
#endif

View File

@@ -1,10 +0,0 @@
#ifndef INTCODE_INTCODE_HPP_
#define INTCODE_INTCODE_HPP_
#include <intcode/Interpreter.hpp>
#include <intcode/Machine.hpp>
#include <intcode/Parser.hpp>
#include <intcode/Startup.hpp>
#include <Overload.hpp>
#endif