|
| 1 | +//! Assembunny interpreter. |
| 2 | +//! |
| 3 | +//! See [`Day12`](crate::Day12) and [`Day23`](crate::Day23). |
| 4 | +
|
| 5 | +use std::marker::PhantomData; |
| 6 | +use utils::prelude::*; |
| 7 | + |
| 8 | +pub(crate) trait InterpreterConfig { |
| 9 | + const SUPPORTS_TOGGLE: bool = false; |
| 10 | +} |
| 11 | + |
| 12 | +#[derive(Clone, Debug)] |
| 13 | +pub(crate) struct Interpreter<C: InterpreterConfig> { |
| 14 | + instructions: Vec<Instruction>, |
| 15 | + phantom: PhantomData<C>, |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(Copy, Clone, PartialEq, Eq, Debug)] |
| 19 | +enum Register { |
| 20 | + A, |
| 21 | + B, |
| 22 | + C, |
| 23 | + D, |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Copy, Clone, PartialEq, Eq, Debug)] |
| 27 | +enum Value { |
| 28 | + Register(Register), |
| 29 | + Number(i32), |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Copy, Clone, PartialEq, Eq, Debug)] |
| 33 | +enum Instruction { |
| 34 | + Copy(Value, Register), |
| 35 | + Increment(Register), |
| 36 | + Decrement(Register), |
| 37 | + JumpIfNotZero(Value, Value), |
| 38 | + Toggle(Register), |
| 39 | + Invalid2(Value, Value), |
| 40 | +} |
| 41 | + |
| 42 | +impl<C: InterpreterConfig> Interpreter<C> { |
| 43 | + pub fn new(input: &str) -> Result<Self, InputError> { |
| 44 | + let register = parser::one_of(( |
| 45 | + b'a'.map(|_| Register::A), |
| 46 | + b'b'.map(|_| Register::B), |
| 47 | + b'c'.map(|_| Register::C), |
| 48 | + b'd'.map(|_| Register::D), |
| 49 | + )); |
| 50 | + let value = register |
| 51 | + .map(Value::Register) |
| 52 | + .or(parser::i32().map(Value::Number)); |
| 53 | + |
| 54 | + Ok(Self { |
| 55 | + instructions: parser::one_of(( |
| 56 | + register.with_prefix("inc ").map(Instruction::Increment), |
| 57 | + register.with_prefix("dec ").map(Instruction::Decrement), |
| 58 | + value |
| 59 | + .with_prefix("cpy ") |
| 60 | + .then(register.with_prefix(" ")) |
| 61 | + .map(|(v, r)| Instruction::Copy(v, r)), |
| 62 | + value |
| 63 | + .with_prefix("jnz ") |
| 64 | + .then(value.with_prefix(" ")) |
| 65 | + .map(|(v, o)| Instruction::JumpIfNotZero(v, o)), |
| 66 | + register.with_prefix("tgl ").map_res(|r| { |
| 67 | + if C::SUPPORTS_TOGGLE { |
| 68 | + Ok(Instruction::Toggle(r)) |
| 69 | + } else { |
| 70 | + Err("toggle instruction not supported") |
| 71 | + } |
| 72 | + }), |
| 73 | + )) |
| 74 | + .parse_lines(input)?, |
| 75 | + phantom: PhantomData, |
| 76 | + }) |
| 77 | + } |
| 78 | + |
| 79 | + pub fn execute(&self, mut reg: [i32; 4]) -> i32 { |
| 80 | + let mut pc = 0; |
| 81 | + |
| 82 | + let mut instructions = self.instructions.clone(); |
| 83 | + while pc < instructions.len() { |
| 84 | + #[rustfmt::skip] // Rustfmt wants each pattern to be on a single really long line |
| 85 | + match instructions[pc..] { |
| 86 | + // Recognize the following pattern of instructions which can be simplified to addition |
| 87 | + // inc $x |
| 88 | + // dec $y |
| 89 | + // jnz $y -2 |
| 90 | + // This is the key optimization for Day 12 |
| 91 | + [ |
| 92 | + Instruction::Increment(x), |
| 93 | + Instruction::Decrement(y), |
| 94 | + Instruction::JumpIfNotZero(Value::Register(y2), Value::Number(-2)), |
| 95 | + .. |
| 96 | + ] if y == y2 => { |
| 97 | + reg[x as usize] += reg[y as usize]; |
| 98 | + reg[y as usize] = 0; |
| 99 | + pc += 3; |
| 100 | + continue; |
| 101 | + } |
| 102 | + // Recognize the following pattern of instructions which can be simplified to multiplication |
| 103 | + // cpy $w $x |
| 104 | + // inc $y |
| 105 | + // dec $x |
| 106 | + // jnz $x -2 |
| 107 | + // dec $z |
| 108 | + // jnz $z -5 |
| 109 | + // This is the key optimisation for Day 23 |
| 110 | + [ |
| 111 | + Instruction::Copy(Value::Register(w), x), |
| 112 | + Instruction::Increment(y), |
| 113 | + Instruction::Decrement(x2), |
| 114 | + Instruction::JumpIfNotZero(Value::Register(x3), Value::Number(-2)), |
| 115 | + Instruction::Decrement(z), |
| 116 | + Instruction::JumpIfNotZero(Value::Register(z2), Value::Number(-5)), |
| 117 | + .. |
| 118 | + ] if x == x2 && x == x3 && z == z2 => { |
| 119 | + reg[y as usize] = reg[w as usize] * reg[z as usize]; |
| 120 | + reg[x as usize] = 0; |
| 121 | + reg[z as usize] = 0; |
| 122 | + pc += 6; |
| 123 | + continue; |
| 124 | + } |
| 125 | + _ => {} |
| 126 | + }; |
| 127 | + |
| 128 | + match instructions[pc] { |
| 129 | + Instruction::Copy(v, dst) => reg[dst as usize] = v.get(®), |
| 130 | + Instruction::Increment(dst) => reg[dst as usize] += 1, |
| 131 | + Instruction::Decrement(dst) => reg[dst as usize] -= 1, |
| 132 | + Instruction::JumpIfNotZero(v, offset) => { |
| 133 | + if v.get(®) != 0 { |
| 134 | + let offset = offset.get(®); |
| 135 | + let Some(new_pc) = pc.checked_add_signed(offset as isize) else { |
| 136 | + break; |
| 137 | + }; |
| 138 | + pc = new_pc; |
| 139 | + continue; |
| 140 | + } |
| 141 | + } |
| 142 | + Instruction::Toggle(r) => 'toggle: { |
| 143 | + let Some(index) = pc.checked_add_signed(reg[r as usize] as isize) else { |
| 144 | + break 'toggle; |
| 145 | + }; |
| 146 | + if index >= instructions.len() { |
| 147 | + break 'toggle; |
| 148 | + } |
| 149 | + |
| 150 | + instructions[index] = match instructions[index] { |
| 151 | + Instruction::Increment(r) => Instruction::Decrement(r), |
| 152 | + Instruction::Decrement(r) | Instruction::Toggle(r) => { |
| 153 | + Instruction::Increment(r) |
| 154 | + } |
| 155 | + Instruction::JumpIfNotZero(v, Value::Register(r)) => { |
| 156 | + Instruction::Copy(v, r) |
| 157 | + } |
| 158 | + Instruction::JumpIfNotZero(v, o @ Value::Number(_)) => { |
| 159 | + Instruction::Invalid2(v, o) |
| 160 | + } |
| 161 | + Instruction::Copy(v, r) => { |
| 162 | + Instruction::JumpIfNotZero(v, Value::Register(r)) |
| 163 | + } |
| 164 | + Instruction::Invalid2(v1, v2) => { |
| 165 | + // Effectively an invalid copy instruction |
| 166 | + Instruction::JumpIfNotZero(v1, v2) |
| 167 | + } |
| 168 | + }; |
| 169 | + } |
| 170 | + Instruction::Invalid2(_, _) => {} |
| 171 | + } |
| 172 | + |
| 173 | + pc += 1; |
| 174 | + } |
| 175 | + |
| 176 | + reg[0] |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +impl Value { |
| 181 | + #[inline] |
| 182 | + fn get(self, registers: &[i32; 4]) -> i32 { |
| 183 | + match self { |
| 184 | + Value::Register(r) => registers[r as usize], |
| 185 | + Value::Number(n) => n, |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments