Skip to main content

aoc/year2019/
day07.rs

1//! # Amplification Circuit
2//!
3//! Brute force solution for both parts using the utility [`permutations`] method to test each of
4//! the possible 5! or 120 permutations of the phase settings.
5use std::array::from_fn;
6
7use crate::util::intcode::*;
8use crate::util::parse::*;
9
10pub fn parse(input: &str) -> Vec<i64> {
11    input.iter_signed().collect()
12}
13
14pub fn part1(input: &[i64]) -> i64 {
15    let mut result = 0;
16    let mut computer = Computer::new(input);
17
18    let mut sequence = |phases: [i64; 5]| {
19        // Send exactly 2 inputs and receive exactly 1 output per amplifier.
20        let signal = phases.iter().fold(0, |signal, &phase| {
21            computer.reset();
22            computer.input(phase);
23            computer.input(signal);
24            let State::Output(next) = computer.run() else { unreachable!() };
25            next
26        });
27
28        result = result.max(signal);
29    };
30
31    permutations([0, 1, 2, 3, 4], 0, &mut sequence);
32    result
33}
34
35pub fn part2(input: &[i64]) -> i64 {
36    let mut result = 0;
37    let mut computers: [_; 5] = from_fn(|_| Computer::new(input));
38
39    let mut feedback = |phases: [i64; 5]| {
40        // Reset state and send each initial phase setting exactly once.
41        for (computer, phase) in computers.iter_mut().zip(phases) {
42            computer.reset();
43            computer.input(phase);
44        }
45
46        // Chain amplifier inputs and outputs in a loop until all threads finish.
47        let mut signal = 0;
48
49        'outer: loop {
50            for computer in &mut computers {
51                computer.input(signal);
52                let State::Output(next) = computer.run() else { break 'outer };
53                signal = next;
54            }
55        }
56
57        result = result.max(signal);
58    };
59
60    permutations([5, 6, 7, 8, 9], 0, &mut feedback);
61    result
62}
63
64/// Generates all possible permutations of the array, passing them to a callback function.
65fn permutations(mut array: [i64; 5], i: usize, f: &mut impl FnMut([i64; 5])) {
66    if i == 5 {
67        f(array);
68    } else {
69        for j in i..5 {
70            array.swap(i, j);
71            permutations(array, i + 1, f);
72            array.swap(i, j);
73        }
74    }
75}