// ==================================================
// > PART 1
// ==================================================
$solution_1 = 0;
foreach (set(range(0, 4))->permutations() as $sequence) {
$is = 0;
foreach ($sequence as $ps) {
$is = (new Intcode($input, [$ps, $is]))->run();
}
$solution_1 = max($is, $solution_1);
}
// ==================================================
// > PART 2
// ==================================================
$solution_2 = 0;
foreach (set(range(5, 9))->permutations() as $sequence) {
$amb = set(range(0, 4))->map(fn ($i) => new Intcode($input, [array_values($sequence)[$i]]));
$amb[0]->input[] = 0;
while (true) {
foreach (range(0, 4) as $m) {
// Run until it stops or waits for an input
// Add its last output to the next machine in the loop
$amb[($m + 1) % 5]->input[] = $amb[$m]->run();
}
// Stop everything once the last machine is completely done
if ($amb[4]->ended) break;
}
$solution_2 = max($solution_2, $amb[4]->output->last());
}