From c3b3de5416c34af7473abde3628326cc87ad4954 Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Sat, 12 Nov 2022 21:22:07 -0800 Subject: [PATCH] 19 --- 2019/19.cpp | 52 +++++++++++++++++++++++++++++ 2019/CMakeLists.txt | 3 ++ intcode/include/intcode/Machine.hpp | 1 + intcode/src/Machine.cpp | 6 ++++ 4 files changed, 62 insertions(+) create mode 100644 2019/19.cpp diff --git a/2019/19.cpp b/2019/19.cpp new file mode 100644 index 0000000..63abf22 --- /dev/null +++ b/2019/19.cpp @@ -0,0 +1,52 @@ +#include + +#include +#include +#include + +using namespace aocpp; +using namespace intcode; + +namespace { + +class Scanner { + Machine machine_; + +public: + Scanner(Machine machine) : machine_{std::move(machine)} {} + + auto operator()(ValueType x, ValueType y) const -> bool { + Machine m {machine_}; + StepInput(m, x); + StepInput(m, y); + return StepOutput(m); + } +}; + +auto Part1(Scanner const& scanner) { + std::int64_t count {}; + for (std::int64_t y = 0; y < 50; y++) { + for (std::int64_t x = 0; x < 50; x++) { + if (scanner(x, y)) { + count++; + } + } + } + return count; +} + +auto Part2(Scanner const& scanner) { + ValueType x = 0; + for(ValueType y = 99;; y++){ + for (; !scanner(x, y); x++); + if (scanner(x+99,y-99)) { return 10000 * x + y-99; } + } +} + +} // namespace + +auto main(int argc, char** argv) -> int { + auto scanner = Scanner{Machine{ParseStream(aocpp::Startup(argc, argv))}}; + std::cout << "Part 1: " << Part1(scanner) << std::endl; + std::cout << "Part 2: " << Part2(scanner) << std::endl; +} diff --git a/2019/CMakeLists.txt b/2019/CMakeLists.txt index 95705e6..7ad5c2d 100644 --- a/2019/CMakeLists.txt +++ b/2019/CMakeLists.txt @@ -49,6 +49,9 @@ target_link_libraries(17 aocpp intcode) add_executable(18 18.cpp) target_link_libraries(18 aocpp) +add_executable(19 19.cpp) +target_link_libraries(19 aocpp intcode) + add_executable(20 20.cpp) target_link_libraries(20 aocpp) diff --git a/intcode/include/intcode/Machine.hpp b/intcode/include/intcode/Machine.hpp index 7cd100a..86e4f2f 100644 --- a/intcode/include/intcode/Machine.hpp +++ b/intcode/include/intcode/Machine.hpp @@ -27,6 +27,7 @@ public: /// @param address /// @return reference to memory at given address auto At(std::size_t address) -> ValueType &; + auto At(std::size_t address) const -> ValueType; /// Access memory at address relative to base pointer /// @param offset from base pointer diff --git a/intcode/src/Machine.cpp b/intcode/src/Machine.cpp index 33cc276..9fc0a9c 100644 --- a/intcode/src/Machine.cpp +++ b/intcode/src/Machine.cpp @@ -11,6 +11,12 @@ auto Machine::At(std::size_t const i) -> ValueType & { return i < rom_.size() ? rom_[i] : ram_[i]; } +auto Machine::At(std::size_t const i) const -> ValueType { + if (i < rom_.size()) return rom_[i]; + if (auto it = ram_.find(i); it != ram_.end()) { return it->second; } + return 0; +} + auto Machine::Rel(std::size_t const i) -> ValueType & { return At(base_ + i); }