70 lines
1.6 KiB
C++
70 lines
1.6 KiB
C++
#include <iostream>
|
|
#include <utility>
|
|
#include <tuple>
|
|
#include <set>
|
|
|
|
#include <intcode/intcode.hpp>
|
|
#include <intcode/Coord.hpp>
|
|
using namespace intcode;
|
|
|
|
namespace {
|
|
|
|
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;
|
|
}
|