#include #include #include #include #include #include #include using namespace intcode; namespace { std::pair const moves[4] {{1, Up}, {2, Down}, {3, Left}, {4, Right}}; auto Fork(Machine m, ValueType cmd) { std::get(Step(m)).pos = cmd; auto o = std::get(Step(m)).val; return std::make_pair(o, std::move(m)); } auto Compute(Machine machine) { Coord const origin {0,0}; std::set world {origin}; std::deque> todo {{0, origin, std::move(machine)}}; long part1 = -1; long part2 = -1; top: while (!todo.empty()) { auto & [steps, loc, m] = todo.front(); part2 = steps; steps++; for (auto&& [cmd, fun] : moves) { auto loc_ = fun(loc); if (world.insert(loc_).second) { auto [o,m_] = Fork(m, cmd); switch (o) { case 2: part1 = steps; world = {origin}; todo = {{0, origin, std::move(m_)}}; goto top; case 1: todo.emplace_back(steps, loc_, std::move(m_)); } } } todo.pop_front(); } return std::make_pair(part1,part2); } } // namespace auto main() -> int { auto [part1, part2] = Compute(Machine{ParseStream(std::cin)}); std::cout << "Part 1: " << part1 << std::endl; std::cout << "Part 2: " << part2 << std::endl; }