aocpp/day13.cpp
2022-11-06 11:33:20 -08:00

65 lines
1.3 KiB
C++

#include <iostream>
#include <stdexcept>
#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;
Run(machine,
[]() -> ValueType { throw std::runtime_error{"unexpected input request"}; },
[&](auto x) {
auto y = StepOutput(machine);
auto v = StepOutput(machine);
if (2 == v) { screen.insert({x,y}); } else { screen.erase({x,y}); }
});
return screen.size();
}
auto Compute2(Machine machine) {
ValueType score {0};
ValueType paddleX {0};
ValueType ballX {0};
machine.At(0) = 2;
Run(machine,
[&]() {
return paddleX < ballX ? 1 : paddleX > ballX ? -1 : 0;
},
[&](auto x) {
auto y = StepOutput(machine);
auto v = StepOutput(machine);
if (-1 == x && 0 == y) {
score = v;
} else {
switch (v) {
case 3: // paddle
paddleX = x;
break;
case 4: // ball
ballX = x;
break;
}
}
});
return score;
}
} // namespace
auto main(int argc, char** argv) -> int {
auto machine = Startup(argc, argv);
std::cout << "Part 1: " << Compute1(machine) << std::endl;
std::cout << "Part 2: " << Compute2(std::move(machine)) << std::endl;
}