#include #include #include #include #include #include #include #include using namespace intcode; namespace { using Coord = std::pair; auto Compute(Machine machine) -> std::pair { Coord here {0,0}; std::map world {{{0,0},1}}; using State = std::tuple; std::vector layer {{{0,0}, std::move(machine)}}; std::vector next_layer; auto action = [&](Machine m, ValueType cmd, Coord coord) { if (!world.contains(coord)) { std::get(Step(m)).pos = cmd; auto o = std::get(Step(m)).val; world[coord] = o; if (o) { next_layer.emplace_back(coord, std::move(m)); } } }; int part1 = 0; while (!layer.empty()) { for (auto && s : layer) { auto& [loc, m] = s; if (2 == world[loc]) { world.clear(); world[loc] = 2; auto start = std::move(m); layer.clear(); layer.push_back({loc, std::move(start)}); goto loop2; } auto [x,y] = loc; action(m, 1, {x,y-1}); action(m, 2, {x,y+1}); action(m, 3, {x-1,y}); action(std::move(m), 4, {x+1,y}); } layer = std::move(next_layer); next_layer.clear(); part1++; } loop2: int part2 = 0; while (!layer.empty()) { for (auto && s : layer) { auto& [loc, m] = s; auto [x,y] = loc; action(m, 1, {x,y-1}); action(m, 2, {x,y+1}); action(m, 3, {x-1,y}); action(std::move(m), 4, {x+1,y}); } layer = std::move(next_layer); next_layer.clear(); part2++; } return {part1, part2-1}; } } // 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; }