From e98a71dad60e01cbe7acf49025cb49b059f1ce45 Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Fri, 4 Nov 2022 09:38:01 -0700 Subject: [PATCH] rearrange --- day02.cpp | 2 +- day05.cpp | 2 +- day07.cpp | 2 +- day09.cpp | 2 +- day11.cpp | 2 +- day13.cpp | 2 +- day15.cpp | 2 +- lib/CMakeLists.txt | 4 +- lib/include/intcode/Interpreter.hpp | 26 ++++++++++++ .../{intcode.hpp => intcode/Machine.hpp} | 35 +++------------- lib/include/intcode/Parser.hpp | 15 +++++++ lib/include/intcode/intcode.hpp | 11 +++++ lib/{intcode.cpp => src/Interpreter.cpp} | 41 +------------------ lib/src/Machine.cpp | 30 ++++++++++++++ lib/src/Parser.cpp | 17 ++++++++ 15 files changed, 115 insertions(+), 78 deletions(-) create mode 100644 lib/include/intcode/Interpreter.hpp rename lib/include/{intcode.hpp => intcode/Machine.hpp} (54%) create mode 100644 lib/include/intcode/Parser.hpp create mode 100644 lib/include/intcode/intcode.hpp rename lib/{intcode.cpp => src/Interpreter.cpp} (65%) create mode 100644 lib/src/Machine.cpp create mode 100644 lib/src/Parser.cpp diff --git a/day02.cpp b/day02.cpp index ee0effc..2358984 100644 --- a/day02.cpp +++ b/day02.cpp @@ -1,7 +1,7 @@ #include #include -#include +#include using namespace intcode; diff --git a/day05.cpp b/day05.cpp index 4e3b4af..92f1dc4 100644 --- a/day05.cpp +++ b/day05.cpp @@ -1,7 +1,7 @@ #include #include -#include +#include using namespace intcode; namespace { diff --git a/day07.cpp b/day07.cpp index 06162a3..06d1c0e 100644 --- a/day07.cpp +++ b/day07.cpp @@ -5,7 +5,7 @@ #include #include -#include +#include using namespace intcode; namespace { diff --git a/day09.cpp b/day09.cpp index 8570af3..0779504 100644 --- a/day09.cpp +++ b/day09.cpp @@ -1,7 +1,7 @@ #include #include -#include +#include using namespace intcode; namespace { diff --git a/day11.cpp b/day11.cpp index 137f6dd..7f98690 100644 --- a/day11.cpp +++ b/day11.cpp @@ -3,7 +3,7 @@ #include #include -#include +#include using namespace intcode; namespace { diff --git a/day13.cpp b/day13.cpp index a16201a..52db32d 100644 --- a/day13.cpp +++ b/day13.cpp @@ -3,7 +3,7 @@ #include #include -#include +#include using namespace intcode; namespace { diff --git a/day15.cpp b/day15.cpp index a351318..d6bcdb1 100644 --- a/day15.cpp +++ b/day15.cpp @@ -6,7 +6,7 @@ #include #include -#include +#include using namespace intcode; namespace { diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 047637c..e10e3fc 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,2 +1,2 @@ -add_library(intcode intcode.cpp) -target_include_directories(intcode PUBLIC include) \ No newline at end of file +add_library(intcode src/Parser.cpp src/Interpreter.cpp src/Machine.cpp) +target_include_directories(intcode PUBLIC include) diff --git a/lib/include/intcode/Interpreter.hpp b/lib/include/intcode/Interpreter.hpp new file mode 100644 index 0000000..bb8967a --- /dev/null +++ b/lib/include/intcode/Interpreter.hpp @@ -0,0 +1,26 @@ +#ifndef INTCODE_INTERPRETER_HPP_ +#define INTCODE_INTERPRETER_HPP_ + +#include "Machine.hpp" + +namespace intcode { + +struct Input { +ValueType &pos; +}; + +struct Output { +ValueType val; +}; + +struct Halt {}; + +auto Step(Machine & m) -> std::variant; + +struct BadInstruction : public std::runtime_error { + explicit BadInstruction(char const* what); +}; + +} // namespace + +#endif diff --git a/lib/include/intcode.hpp b/lib/include/intcode/Machine.hpp similarity index 54% rename from lib/include/intcode.hpp rename to lib/include/intcode/Machine.hpp index a78dd7f..2066f98 100644 --- a/lib/include/intcode.hpp +++ b/lib/include/intcode/Machine.hpp @@ -1,30 +1,13 @@ -#ifndef INTCODE_HPP_ -#define INTCODE_HPP_ +#ifndef INTCODE_MACHINE_HPP_ +#define INTCODE_MACHINE_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 {}; +using ValueType = std::int64_t; class Machine { std::vector rom_; @@ -51,14 +34,6 @@ public: auto Rebase(std::size_t offset) -> void; }; -auto Step(Machine & m) -> std::variant; +} // namespace -struct BadInstruction : public std::runtime_error { - explicit BadInstruction(char const* what); -}; - -auto ParseStream(std::istream &in) -> std::vector; - -} - -#endif // INTCODE_HPP_ +#endif diff --git a/lib/include/intcode/Parser.hpp b/lib/include/intcode/Parser.hpp new file mode 100644 index 0000000..e696863 --- /dev/null +++ b/lib/include/intcode/Parser.hpp @@ -0,0 +1,15 @@ +#ifndef INTCODE_PARSER_HPP_ +#define INTCODE_PARSER_HPP_ + +#include +#include + +#include "Machine.hpp" + +namespace intcode { + +auto ParseStream(std::istream &in) -> std::vector; + +} // namespace + +#endif diff --git a/lib/include/intcode/intcode.hpp b/lib/include/intcode/intcode.hpp new file mode 100644 index 0000000..a56803b --- /dev/null +++ b/lib/include/intcode/intcode.hpp @@ -0,0 +1,11 @@ +#ifndef INTCODE_HPP_ +#define INTCODE_INTCODE_HPP_ + +#include +#include +#include + +template struct overloaded : Ts... { using Ts::operator()...; }; +template overloaded(Ts...) -> overloaded; + +#endif diff --git a/lib/intcode.cpp b/lib/src/Interpreter.cpp similarity index 65% rename from lib/intcode.cpp rename to lib/src/Interpreter.cpp index 00e1c2b..59df270 100644 --- a/lib/intcode.cpp +++ b/lib/src/Interpreter.cpp @@ -1,37 +1,10 @@ -#include "intcode.hpp" -#include +#include namespace intcode { BadInstruction::BadInstruction(char const* what) : std::runtime_error{what} {} -Machine::Machine() : rom_{}, ram_{}, pc_{0}, base_{0} {} - -Machine::Machine(std::vector ram) - : rom_{ram}, ram_{}, pc_{0}, base_{0} {} - -auto Machine::At(std::size_t i) -> ValueType & { - return i < rom_.size() ? rom_[i] : ram_[i]; -} - -auto Machine::Rel(std::size_t i) -> ValueType & { - return At(base_ + i); -} - -auto Machine::Rebase(std::size_t offset) -> void { - base_ += offset; -} - - -auto Machine::Next() -> ValueType & { - return At(pc_++); -} - -auto Machine::Goto(std::size_t address) -> void { - pc_ = address; -} - auto Step(Machine & m) -> std::variant { for (;;) { auto instruction = m.Next(); @@ -119,14 +92,4 @@ auto Step(Machine & m) -> std::variant { } } -auto ParseStream(std::istream &in) -> std::vector { - ValueType x; - std::string str; - std::vector result; - while (std::getline(in, str, ',')) { - result.push_back(std::stol(str)); - } - return result; -} - -} +} // namespace diff --git a/lib/src/Machine.cpp b/lib/src/Machine.cpp new file mode 100644 index 0000000..e7737cf --- /dev/null +++ b/lib/src/Machine.cpp @@ -0,0 +1,30 @@ +#include + +namespace intcode { + +Machine::Machine() : rom_{}, ram_{}, pc_{0}, base_{0} {} + +Machine::Machine(std::vector ram) + : rom_{ram}, ram_{}, pc_{0}, base_{0} {} + +auto Machine::At(std::size_t i) -> ValueType & { + return i < rom_.size() ? rom_[i] : ram_[i]; +} + +auto Machine::Rel(std::size_t i) -> ValueType & { + return At(base_ + i); +} + +auto Machine::Rebase(std::size_t offset) -> void { + base_ += offset; +} + +auto Machine::Next() -> ValueType & { + return At(pc_++); +} + +auto Machine::Goto(std::size_t address) -> void { + pc_ = address; +} + +} // namespace diff --git a/lib/src/Parser.cpp b/lib/src/Parser.cpp new file mode 100644 index 0000000..cf489e7 --- /dev/null +++ b/lib/src/Parser.cpp @@ -0,0 +1,17 @@ +#include + +#include + +namespace intcode { + +auto ParseStream(std::istream &in) -> std::vector { + ValueType x; + std::string str; + std::vector result; + while (std::getline(in, str, ',')) { + result.push_back(std::stol(str)); + } + return result; +} + +} // namespace