78 lines
1.9 KiB
C++
78 lines
1.9 KiB
C++
#include <iostream>
|
|
#include <utility>
|
|
#include <vector>
|
|
#include <algorithm>
|
|
|
|
#include <intcode.hpp>
|
|
|
|
namespace {
|
|
auto compute(intcode machine, std::vector<std::int64_t> const& params) -> std::int64_t {
|
|
|
|
std::vector<intcode> amps;
|
|
|
|
for (auto p : params) {
|
|
auto m = machine;
|
|
auto i = std::get<Input>(m.step());
|
|
i.pos = p;
|
|
amps.emplace_back(std::move(m));
|
|
}
|
|
|
|
std::int64_t prev = 0;
|
|
|
|
for (auto& m : amps) {
|
|
auto i = std::get<Input>(m.step());
|
|
i.pos = prev;
|
|
auto o = std::get<Output>(m.step());
|
|
prev = o.val;
|
|
}
|
|
|
|
return prev;
|
|
}
|
|
auto compute2(intcode machine, std::vector<std::int64_t> const& params) -> std::int64_t {
|
|
|
|
std::vector<intcode> amps;
|
|
|
|
for (auto p : params) {
|
|
auto m = machine;
|
|
auto i = std::get<Input>(m.step());
|
|
i.pos = p;
|
|
amps.emplace_back(std::move(m));
|
|
}
|
|
|
|
std::int64_t last = 0;
|
|
std::int64_t prev = 0;
|
|
|
|
for(;;) {
|
|
for (auto& m : amps) {
|
|
auto i = m.step();
|
|
if (auto p = std::get_if<Input>(&i)) {
|
|
p->pos = prev;
|
|
prev = std::get<Output>(m.step()).val;
|
|
} else {
|
|
return last;
|
|
}
|
|
}
|
|
last = prev;
|
|
}
|
|
}
|
|
}
|
|
|
|
auto main() -> int {
|
|
auto machine = intcode{parse_stream(std::cin)};
|
|
{
|
|
std::vector<std::int64_t> params {0,1,2,3,4};
|
|
std::int64_t best = 0;
|
|
do {
|
|
best = std::max(best, compute(machine, params));
|
|
} while (std::next_permutation(params.begin(), params.end()));
|
|
std::cout << "Part 1: " << best << std::endl;
|
|
}
|
|
{
|
|
std::vector<std::int64_t> params {5,6,7,8,9};
|
|
std::int64_t best = 0;
|
|
do {
|
|
best = std::max(best, compute2(machine, params));
|
|
} while (std::next_permutation(params.begin(), params.end()));
|
|
std::cout << "Part 2: " << best << std::endl;
|
|
}
|
|
} |