aocpp/day09.cpp

32 lines
666 B
C++
Raw Normal View History

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 output = -1;
while (std::visit(overloaded{
[](Halt) { return false; },
[d](Input arg) {
arg.pos = d;
return true;
},
[&output](Output arg) {
output = arg.val;
return false;
}},
machine.step()));
return 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), 2) << std::endl;
}