53 lines
1.1 KiB
C++
53 lines
1.1 KiB
C++
#include <iostream>
|
|
#include <utility>
|
|
|
|
#include <doctest.h>
|
|
|
|
#include <aocpp/Startup.hpp>
|
|
#include <intcode/intcode.hpp>
|
|
|
|
|
|
using namespace intcode;
|
|
|
|
namespace {
|
|
|
|
auto Compute(Machine machine, ValueType x, ValueType y) {
|
|
machine.At(1) = x;
|
|
machine.At(2) = y;
|
|
Step(machine);
|
|
return machine.At(0);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_SUITE("documented examples") {
|
|
|
|
auto eval = [](std::vector<ValueType> program, std::vector<ValueType> output) {
|
|
Machine m{program};
|
|
REQUIRE(std::holds_alternative<Halt>(Step(m)));
|
|
REQUIRE(m.Dump() == output);
|
|
};
|
|
|
|
TEST_CASE("part 1") {
|
|
eval({1,0,0,0,99}, {2,0,0,0,99});
|
|
eval({2,3,0,3,99}, {2,3,0,6,99});
|
|
eval({2,4,4,5,99,0}, {2,4,4,5,99,9801});
|
|
eval({1,1,1,4,99,5,6,0,99}, {30,1,1,4,2,5,6,0,99});
|
|
}
|
|
}
|
|
|
|
auto Main(std::istream & in, std::ostream & out) -> void
|
|
{
|
|
auto machine = Machine{ParseStream(in)};
|
|
|
|
out << "Part 1: " << Compute(machine, 12, 2) << std::endl;
|
|
|
|
for (std::int64_t i = 0; i < 100; i++) {
|
|
for (std::int64_t j = 0; j < 100; j++) {
|
|
if (19690720 == Compute(machine, i, j)) {
|
|
out << "Part 2: " << 100 * i + j << std::endl;
|
|
}
|
|
}
|
|
}
|
|
}
|