2022-11-03 08:15:17 -07:00
|
|
|
#include <iostream>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include <intcode.hpp>
|
2022-11-03 12:53:10 -07:00
|
|
|
using namespace intcode;
|
2022-11-03 08:15:17 -07:00
|
|
|
|
|
|
|
namespace {
|
2022-11-03 12:53:10 -07:00
|
|
|
|
|
|
|
auto compute(Machine machine, value_type d) -> value_type {
|
|
|
|
value_type last_output = -1;
|
|
|
|
while (std::visit(overloaded{
|
|
|
|
[](Halt) { return false; },
|
|
|
|
[d, &machine](Input arg) {
|
|
|
|
arg.pos = d;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
[&last_output](Output arg) {
|
|
|
|
last_output = arg.val;
|
|
|
|
return true;
|
|
|
|
}},
|
|
|
|
machine.step()));
|
|
|
|
return last_output;
|
2022-11-03 08:15:17 -07:00
|
|
|
}
|
|
|
|
|
2022-11-03 12:53:10 -07:00
|
|
|
} // namespace
|
|
|
|
|
2022-11-03 08:15:17 -07:00
|
|
|
auto main() -> int {
|
2022-11-03 12:53:10 -07:00
|
|
|
auto machine = Machine{parse_stream(std::cin)};
|
|
|
|
std::cout << "Part 1: " << compute(machine, 1) << std::endl;
|
|
|
|
std::cout << "Part 2: " << compute(std::move(machine), 5) << std::endl;
|
|
|
|
}
|