aocpp/day15.cpp
Eric Mertens 29a840bdfc 15
2022-11-04 17:39:35 -07:00

66 lines
1.4 KiB
C++

#include <deque>
#include <iostream>
#include <set>
#include <tuple>
#include <utility>
#include <intcode/intcode.hpp>
#include <intcode/Grid.hpp>
using namespace intcode;
namespace {
std::pair<ValueType, Coord(*)(Coord)> const moves[4]
{{1, Up}, {2, Down}, {3, Left}, {4, Right}};
auto Fork(Machine m, ValueType cmd) {
std::get<Input>(Step(m)).pos = cmd;
auto o = std::get<Output>(Step(m)).val;
return std::make_pair(o, std::move(m));
}
auto Compute(Machine machine) {
Coord const origin {0,0};
std::set<Coord> world {origin};
std::deque<std::tuple<int, Coord, Machine>> 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;
}