From b3f7431be4a7e380695b1e060f061d8ca63d5b8e Mon Sep 17 00:00:00 2001 From: kageru Date: Fri, 13 Dec 2019 10:44:13 +0100 Subject: [PATCH] Intcode: add method for single step execution --- 2019/intcode/src/intcomputer.rs | 26 +++++++++++++++++++------- 2019/intcode/src/lib.rs | 1 + 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/2019/intcode/src/intcomputer.rs b/2019/intcode/src/intcomputer.rs index 3493c91..809f25f 100644 --- a/2019/intcode/src/intcomputer.rs +++ b/2019/intcode/src/intcomputer.rs @@ -9,6 +9,7 @@ pub struct IntComputer { pub enum IntComputerResult { Halt, Output(i64), + Continue, } impl IntComputerResult { @@ -16,6 +17,7 @@ impl IntComputerResult { match self { IntComputerResult::Halt => panic!("Attempted to get value of halt operation"), IntComputerResult::Output(o) => o, + IntComputerResult::Continue => panic!("Attempted to get value of non-output operation"), } } } @@ -31,15 +33,25 @@ impl IntComputer { } } + pub fn step(&mut self) -> IntComputerResult { + match self.cmd_buffer.pop().unwrap_or_else(|| self.decode_next()) { + Operation::Halt {} => return IntComputerResult::Halt, + op => { + if let Some(o) = self.execute(op.to_owned()) { + return IntComputerResult::Output(o); + } else { + return IntComputerResult::Continue; + } + } + } + } + pub fn run(&mut self) -> IntComputerResult { loop { - match self.cmd_buffer.pop().unwrap_or_else(|| self.decode_next()) { - Operation::Halt {} => return IntComputerResult::Halt, - op => { - if let Some(o) = self.execute(op.to_owned()) { - return IntComputerResult::Output(o); - } - } + match self.step() { + IntComputerResult::Halt => return IntComputerResult::Halt, + IntComputerResult::Output(o) => return IntComputerResult::Output(o), + IntComputerResult::Continue => (), } } } diff --git a/2019/intcode/src/lib.rs b/2019/intcode/src/lib.rs index 4e08dea..23aad06 100644 --- a/2019/intcode/src/lib.rs +++ b/2019/intcode/src/lib.rs @@ -22,6 +22,7 @@ pub fn run_for_input(input: &Vec, acc: &mut i64, amp_phases: Vec) -> i match amplifier.run() { IntComputerResult::Output(output) => *acc = output, IntComputerResult::Halt => break, + IntComputerResult::Continue => unreachable!() } } *acc