aocpp/2019/13.cpp

68 lines
1.4 KiB
C++
Raw Permalink 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
2023-01-31 09:15:15 -08:00
auto Main(std::istream & in, std::ostream & out) -> void
2023-01-31 08:58:42 -08:00
{
auto machine = Machine{ParseStream(in)};
2023-01-31 09:15:15 -08:00
out << "Part 1: " << Compute1(machine) << std::endl;
out << "Part 2: " << Compute2(std::move(machine)) << std::endl;
2022-11-03 21:41:06 -07:00
}