1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use crate::assembunny::Interpreter;
use utils::prelude::*;

/// Interpreting assembunny assembly, again.
///
/// The key optimization is that
/// ```text
///  cpy $r1 $r2
///  inc $r3
///  dec $r2
///  jnz $r2 -2
///  dec $r4
///  jnz $r4 -5
/// ```
/// can be replaced with `$r3 = $r1 * r3` followed by `$r2 = 0` and `$r4 = 0`. This reduces the
/// number of simulated cycles for part 2 ~2,000,000 times when using the previously implemented
/// addition optimization, and ~6,000,000 times compared to a naive implementation.
///
/// See also [2016 day 12](crate::Day12).
#[derive(Clone, Debug)]
pub struct Day23 {
    interpreter: Interpreter<true, false>,
}

impl Day23 {
    pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
        Ok(Self {
            interpreter: Interpreter::new(input)?,
        })
    }

    #[must_use]
    pub fn part1(&self) -> i32 {
        self.interpreter.execute([7, 0, 0, 0])
    }

    #[must_use]
    pub fn part2(&self) -> i32 {
        self.interpreter.execute([12, 0, 0, 0])
    }
}

examples!(Day23 -> (i32, i32) [
    {
        input: "cpy 2 a\n\
            tgl a\n\
            tgl a\n\
            tgl a\n\
            cpy 1 a\n\
            dec a\n\
            dec a",
        part1: 3,
    },
]);