From 1d164979c69e6d028c38d5f691df43d98c68f0c9 Mon Sep 17 00:00:00 2001 From: kageru Date: Fri, 13 Dec 2019 10:33:16 +0100 Subject: [PATCH] Add peek for next operation to intcode --- 2019/intcode/src/intcomputer.rs | 10 ++++++++- 2019/intcode/src/tests.rs | 36 +++++++++------------------------ 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/2019/intcode/src/intcomputer.rs b/2019/intcode/src/intcomputer.rs index 425fc44..3493c91 100644 --- a/2019/intcode/src/intcomputer.rs +++ b/2019/intcode/src/intcomputer.rs @@ -52,6 +52,14 @@ impl IntComputer { outputs } + pub fn peek_operation(&mut self) -> Operation { + if self.cmd_buffer.is_empty() { + let next = self.decode_next(); + self.cmd_buffer.push(next); + } + self.cmd_buffer[0].clone() + } + #[rustfmt::skip] fn get_next(&mut self, mode: Mode) -> i64 { let value = *self.tape.get(self.pos as usize).unwrap(); @@ -179,7 +187,7 @@ fn get_mode(raw_opcode: &[char], pos: ParameterPosition) -> Mode { } #[derive(Debug, Clone)] -enum Operation { +pub enum Operation { Add { x: i64, y: i64, diff --git a/2019/intcode/src/tests.rs b/2019/intcode/src/tests.rs index 48d86c0..54ee721 100644 --- a/2019/intcode/src/tests.rs +++ b/2019/intcode/src/tests.rs @@ -93,19 +93,11 @@ fn test_position_equals() { #[test] fn test_immediate_less_than() { assert_eq!( - run_for_input( - &parse_test_input("3,3,1107,-1,8,3,4,3,99"), - &mut 0, - vec![8] - ), + run_for_input(&parse_test_input("3,3,1107,-1,8,3,4,3,99"), &mut 0, vec![8]), 0 ); assert_eq!( - run_for_input( - &parse_test_input("3,3,1107,-1,8,3,4,3,99"), - &mut 0, - vec![7] - ), + run_for_input(&parse_test_input("3,3,1107,-1,8,3,4,3,99"), &mut 0, vec![7]), 1 ); } @@ -113,19 +105,11 @@ fn test_immediate_less_than() { #[test] fn test_immediate_equals() { assert_eq!( - run_for_input( - &parse_test_input("3,3,1108,-1,8,3,4,3,99"), - &mut 0, - vec![8] - ), + run_for_input(&parse_test_input("3,3,1108,-1,8,3,4,3,99"), &mut 0, vec![8]), 1 ); assert_eq!( - run_for_input( - &parse_test_input("3,3,1108,-1,8,3,4,3,99"), - &mut 0, - vec![7] - ), + run_for_input(&parse_test_input("3,3,1108,-1,8,3,4,3,99"), &mut 0, vec![7]), 0 ); } @@ -133,14 +117,14 @@ fn test_immediate_equals() { #[test] fn test_quine() { assert_eq!( - run_for_input( - &parse_test_input("109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99"), - &mut 0, + IntComputer::new( + parse_test_input("109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99"), + 0, vec![0] - ), - 99 + ) + .get_all_outputs(), + vec![109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99] ); - //"109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99" } #[test]