67 lines
1.4 KiB
C++
67 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(int argc, char** argv) -> int {
|
|
auto machine = Machine{ParseStream(*aocpp::Startup(argc, argv))};
|
|
std::cout << "Part 1: " << Compute1(machine) << std::endl;
|
|
std::cout << "Part 2: " << Compute2(std::move(machine)) << std::endl;
|
|
}
|