aocpp/day13.cpp

70 lines
1.6 KiB
C++
Raw Normal View History

2022-11-03 21:41:06 -07:00
#include <iostream>
#include <utility>
#include <tuple>
#include <set>
2022-11-04 09:38:01 -07:00
#include <intcode/intcode.hpp>
2022-11-04 17:39:35 -07:00
#include <intcode/Grid.hpp>
2022-11-03 21:41:06 -07:00
using namespace intcode;
namespace {
auto Compute1(Machine machine)
-> std::size_t {
std::set<Coord> screen;
for (;;) {
2022-11-04 08:29:02 -07:00
auto effect = Step(machine);
2022-11-03 21:41:06 -07:00
if (std::holds_alternative<Halt>(effect)) {
return screen.size();
}
auto x = std::get<Output>(effect).val;
2022-11-04 08:29:02 -07:00
auto y = std::get<Output>(Step(machine)).val;
auto v = std::get<Output>(Step(machine)).val;
2022-11-03 21:41:06 -07:00
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(;;) {
2022-11-04 08:29:02 -07:00
auto effect = Step(machine);
2022-11-03 21:41:06 -07:00
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;
2022-11-04 08:29:02 -07:00
auto y = std::get<Output>(Step(machine)).val;
auto v = std::get<Output>(Step(machine)).val;
2022-11-03 21:41:06 -07:00
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;
}