aocpp/2019/13.cpp

67 lines
1.4 KiB
C++
Raw Normal View History

2022-11-03 21:41:06 -07:00
#include <iostream>
2022-11-05 15:06:54 -07:00
#include <stdexcept>
2022-11-03 21:41:06 -07:00
#include <utility>
#include <tuple>
#include <set>
#include <aocpp/Startup.hpp>
#include <aocpp/Coord.hpp>
2022-11-04 09:38:01 -07:00
#include <intcode/intcode.hpp>
using namespace aocpp;
2022-11-03 21:41:06 -07:00
using namespace intcode;
namespace {
auto Compute1(Machine machine)
-> std::size_t {
std::set<Coord> screen;
2022-11-05 15:06:54 -07:00
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();
2022-11-03 21:41:06 -07:00
}
auto Compute2(Machine machine) {
ValueType score {0};
ValueType paddleX {0};
ValueType ballX {0};
machine.At(0) = 2;
2022-11-05 15:06:54 -07:00
Run(machine,
[&]() {
return paddleX < ballX ? 1 : paddleX > ballX ? -1 : 0;
},
[&](auto x) {
auto y = StepOutput(machine);
auto v = StepOutput(machine);
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;
}
}
2022-11-05 15:06:54 -07:00
});
return score;
2022-11-03 21:41:06 -07:00
}
} // namespace
2022-11-06 11:33:20 -08:00
auto main(int argc, char** argv) -> int {
2022-11-07 21:00:14 -08:00
auto machine = Machine{ParseStream(aocpp::Startup(argc, argv))};
2022-11-03 21:41:06 -07:00
std::cout << "Part 1: " << Compute1(machine) << std::endl;
std::cout << "Part 2: " << Compute2(std::move(machine)) << std::endl;
}