aocpp/day13.cpp
2022-11-04 08:29:02 -07:00

71 lines
1.6 KiB
C++

#include <iostream>
#include <utility>
#include <tuple>
#include <set>
#include <intcode.hpp>
using namespace intcode;
namespace {
using Coord = std::pair<ValueType, ValueType>;
auto Compute1(Machine machine)
-> std::size_t {
std::set<Coord> screen;
for (;;) {
auto effect = Step(machine);
if (std::holds_alternative<Halt>(effect)) {
return screen.size();
}
auto x = std::get<Output>(effect).val;
auto y = std::get<Output>(Step(machine)).val;
auto v = std::get<Output>(Step(machine)).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 = Step(machine);
if (std::holds_alternative<Halt>(effect)) {
return score;
} else if (auto i = std::get_if<Input>(&effect)) {
i->pos = paddleX < ballX ? 1
: paddleX > ballX ? -1 : 0;
} else {
auto x = std::get<Output>(effect).val;
auto y = std::get<Output>(Step(machine)).val;
auto v = std::get<Output>(Step(machine)).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;
}