1use crate::intcode::Interpreter;
2use crate::intcode::features::Day09Features;
3use utils::prelude::*;
4
5#[derive(Clone, Debug)]
7pub struct Day21 {
8 interpreter: Interpreter,
9}
10
11const WALK_SCRIPT: &[u8] = b"OR A J\n\
14AND B J\n\
15AND C J\n\
16NOT J J\n\
17AND D J\n\
18WALK\n";
19
20const RUN_SCRIPT: &[u8] = b"NOT H J\n\
27OR C J\n\
28AND B J\n\
29AND A J\n\
30NOT J J\n\
31AND D J\n\
32RUN\n";
33
34impl Day21 {
35 pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
36 Ok(Self {
37 interpreter: Interpreter::parse(input, 1)?,
38 })
39 }
40
41 #[must_use]
42 pub fn part1(&self) -> i64 {
43 self.run(WALK_SCRIPT)
44 }
45
46 #[must_use]
47 pub fn part2(&self) -> i64 {
48 self.run(RUN_SCRIPT)
49 }
50
51 fn run(&self, script: &[u8]) -> i64 {
52 let mut interpreter = self.interpreter.clone();
53 interpreter.push_bytes(script);
54
55 loop {
56 if let x @ 128.. = interpreter.expect_output::<Day09Features>() {
57 return x;
58 }
59 }
60 }
61}
62
63examples!(Day21 -> (i64, i64) []);