aocpp/day15.cpp
Eric Mertens 8c6d8d4a29 white
2022-11-04 11:37:32 -07:00

86 lines
1.9 KiB
C++

#include <iterator>
#include <iostream>
#include <utility>
#include <tuple>
#include <map>
#include <deque>
#include <vector>
#include <intcode/intcode.hpp>
using namespace intcode;
namespace {
using Coord = std::pair<ValueType, ValueType>;
auto Compute(Machine machine) -> std::pair<int,int> {
Coord here {0,0};
std::map<Coord, ValueType> world {{{0,0},1}};
using State = std::tuple<Coord, Machine>;
std::vector<State> layer {{{0,0}, std::move(machine)}};
std::vector<State> next_layer;
auto action = [&](Machine m, ValueType cmd, Coord coord) {
if (!world.contains(coord)) {
std::get<Input>(Step(m)).pos = cmd;
auto o = std::get<Output>(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;
}