aocpp/day05.cpp

32 lines
691 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
2022-11-03 14:43:47 -07:00
auto Compute(Machine machine, ValueType d) -> ValueType {
ValueType last_output = -1;
2022-11-03 12:53:10 -07:00
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;
}},
2022-11-03 14:43:47 -07:00
machine.Step()));
2022-11-03 12:53:10 -07:00
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 14:43:47 -07:00
auto machine = Machine{ParseStream(std::cin)};
std::cout << "Part 1: " << Compute(machine, 1) << std::endl;
std::cout << "Part 2: " << Compute(std::move(machine), 5) << std::endl;
2022-11-03 12:53:10 -07:00
}