#include #include #include #include #include namespace { auto compute(intcode machine, std::vector const& params) -> std::int64_t { std::vector amps; for (auto p : params) { auto m = machine; auto i = std::get(m.step()); i.pos = p; amps.emplace_back(std::move(m)); } std::int64_t prev = 0; for (auto& m : amps) { auto i = std::get(m.step()); i.pos = prev; auto o = std::get(m.step()); prev = o.val; } return prev; } auto compute2(intcode machine, std::vector const& params) -> std::int64_t { std::vector amps; for (auto p : params) { auto m = machine; auto i = std::get(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(&i)) { p->pos = prev; prev = std::get(m.step()).val; } else { return last; } } last = prev; } } } auto main() -> int { auto machine = intcode{parse_stream(std::cin)}; { std::vector 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 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; } }