From e88863ed375ac633159943d38c3af07add2dbe5d Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Thu, 3 Nov 2022 21:41:06 -0700 Subject: [PATCH] 15 --- CMakeLists.txt | 6 +++++ day13.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ day15.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 day13.cpp create mode 100644 day15.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f09d81b..8605f97 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,3 +22,9 @@ target_link_libraries(day09 intcode) add_executable(day11 day11.cpp) target_link_libraries(day11 intcode) + +add_executable(day13 day13.cpp) +target_link_libraries(day13 intcode) + +add_executable(day15 day15.cpp) +target_link_libraries(day15 intcode) diff --git a/day13.cpp b/day13.cpp new file mode 100644 index 0000000..0bb37e3 --- /dev/null +++ b/day13.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include + +#include +using namespace intcode; + +namespace { + +using Coord = std::pair; + +auto Compute1(Machine machine) + -> std::size_t { + std::set screen; + + for (;;) { + auto effect = machine.Step(); + if (std::holds_alternative(effect)) { + return screen.size(); + } + auto x = std::get(effect).val; + auto y = std::get(machine.Step()).val; + auto v = std::get(machine.Step()).val; + + if (2 == v) { screen.insert({x,y}); } else { screen.erase({x,y}); } + } +} + +auto Compute2(Machine machine) { + ValueType score {0}; + ValueType paddleX {0}; + ValueType ballX {0}; + + machine.At(0) = 2; + for(;;) { + auto effect = machine.Step(); + if (std::holds_alternative(effect)) { + return score; + } else if (auto i = std::get_if(&effect)) { + i->pos = paddleX < ballX ? 1 + : paddleX > ballX ? -1 : 0; + } else { + auto x = std::get(effect).val; + auto y = std::get(machine.Step()).val; + auto v = std::get(machine.Step()).val; + + if (-1 == x && 0 == y) { + score = v; + } else { + switch (v) { + case 3: // paddle + paddleX = x; + break; + case 4: // ball + ballX = x; + break; + } + } + } + } +} + +} // namespace + +auto main() -> int { + auto machine = Machine{ParseStream(std::cin)}; + std::cout << "Part 1: " << Compute1(machine) << std::endl; + std::cout << "Part 2: " << Compute2(std::move(machine)) << std::endl; +} diff --git a/day15.cpp b/day15.cpp new file mode 100644 index 0000000..b49d479 --- /dev/null +++ b/day15.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include + +#include +using namespace intcode; + +namespace { + +using Coord = std::pair; + +auto Compute(Machine machine) -> std::pair { + int part1 = 0; + int part2 = 0; + + Coord here {0,0}; + std::map world; + + std::deque> todo {{0, {0,0}, std::move(machine)}}; + + auto action = [&world, &todo](Machine m, int steps, ValueType cmd, Coord coord) { + if (!world.contains(coord)) { + std::get(m.Step()).pos = cmd; + auto o = std::get(m.Step()).val; + world[coord] = o; + if (o) { + todo.emplace_back(steps + 1, coord, std::move(m)); + } + } + }; + + while (!todo.empty()) { + auto [steps, here, machine] = todo.front(); + auto [x,y] = here; + todo.pop_front(); + + if (2 == world[here]) { + part1 = steps; + todo.clear(); + world.clear(); + world[here] = 2; + steps = 0; + part2 = 0; + } + part2 = std::max(part2, steps); + + action(machine, steps, 1, {x,y-1}); + action(machine, steps, 2, {x,y+1}); + action(machine, steps, 3, {x-1,y}); + action(std::move(machine), steps, 4, {x+1,y}); + } + return {part1,part2}; +} + +} // namespace + +auto main() -> int { + auto machine = Machine{ParseStream(std::cin)}; + auto [part1, part2] = Compute(machine); + std::cout << "Part 1: " << part1 << std::endl; + std::cout << "Part 2: " << part2 << std::endl; +}