aocpp/2019/13.cpp
2023-01-31 09:15:25 -08:00

68 lines
1.4 KiB
C++

#include <iostream>
#include <stdexcept>
#include <utility>
#include <tuple>
#include <set>
#include <aocpp/Startup.hpp>
#include <aocpp/Coord.hpp>
#include <intcode/intcode.hpp>
using namespace aocpp;
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(std::istream & in, std::ostream & out) -> void
{
auto machine = Machine{ParseStream(in)};
out << "Part 1: " << Compute1(machine) << std::endl;
out << "Part 2: " << Compute2(std::move(machine)) << std::endl;
}