diff --git a/icc/Cargo.lock b/icc/Cargo.lock index 277f5fb..d95ca2c 100644 --- a/icc/Cargo.lock +++ b/icc/Cargo.lock @@ -4,4 +4,4 @@ version = 3 [[package]] name = "icc" -version = "0.1.0" +version = "1.0.0" diff --git a/icc/Cargo.toml b/icc/Cargo.toml index f2620c8..3efbd7a 100644 --- a/icc/Cargo.toml +++ b/icc/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "icc" -version = "0.1.0" +version = "1.0.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/icc/src/lib.rs b/icc/src/lib.rs index d294236..aa0a9df 100644 --- a/icc/src/lib.rs +++ b/icc/src/lib.rs @@ -1,5 +1,20 @@ -pub fn simulate(v: &mut Vec, input: i32, output: &mut i32) -> Option { - let mut pos = 0; +use std::collections::VecDeque; + +#[derive(Debug)] +#[derive(PartialEq)] +pub enum Status { + Running, + Finished, + WaitingForInput, +} + +pub struct Result { + pub status: Status, + pub instruction: usize, +} + +pub fn simulate(v: &mut Vec, input: &mut VecDeque, output: &mut Vec, start: usize) -> Result { + let mut pos = start; loop { let mut code = v[pos]; // Opcodes are ABCDE, where the op is DE @@ -57,7 +72,15 @@ pub fn simulate(v: &mut Vec, input: i32, output: &mut i32) -> Option { else if op == 3 { // Input let dest = v[pos+1] as usize; - v[dest] = input; + v[dest] = if input.len() == 0 { + return Result { + status: Status::WaitingForInput, + instruction: pos + }; + } else { + input.pop_front().expect("Input stack empty... weird") + }; + println!("Input: {}", v[dest]); pos += 2; } else if op == 4 { @@ -68,8 +91,8 @@ pub fn simulate(v: &mut Vec, input: i32, output: &mut i32) -> Option { else { v[v[pos+1] as usize] }; - println!("{}", d); - *output = d; + println!("Output: {}", d); + output.push(d); pos += 2; } else if op == 5 { @@ -155,7 +178,10 @@ pub fn simulate(v: &mut Vec, input: i32, output: &mut i32) -> Option { panic!("Unknown opcode: {}", code); } } - return None; + return Result { + status: Status::Finished, + instruction: 0 + }; } #[cfg(test)] @@ -164,32 +190,36 @@ mod tests { #[test] fn test_op_1() { let mut program = vec![1, 0, 0, 0, 99]; - let mut output: i32 = 0; - simulate(&mut program, 0, &mut output); + let mut input: VecDeque = VecDeque::new(); + let mut output: Vec = Vec::new(); + simulate(&mut program, &mut input, &mut output, 0); assert_eq!(program[0], 2); } #[test] fn test_op_2() { let mut program = vec![2, 3, 0, 3, 99]; - let mut output: i32 = 0; - simulate(&mut program, 0, &mut output); + let mut input: VecDeque = VecDeque::new(); + let mut output: Vec = Vec::new(); + simulate(&mut program, &mut input, &mut output, 0); assert_eq!(program[3], 6); } #[test] fn test_case_3() { let mut program = vec![2,4,4,5,99,0]; - let mut output: i32 = 0; - simulate(&mut program, 0, &mut output); + let mut input: VecDeque = VecDeque::new(); + let mut output: Vec = Vec::new(); + simulate(&mut program, &mut input, &mut output, 0); assert_eq!(program[program.len() - 1], 9801); } #[test] fn test_case_4() { let mut program = vec![1,1,1,4,99,5,6,0,99]; - let mut output: i32 = 0; - simulate(&mut program, 0, &mut output); + let mut input: VecDeque = VecDeque::new(); + let mut output: Vec = Vec::new(); + simulate(&mut program, &mut input, &mut output, 0); assert_eq!(program[0], 30); assert_eq!(program[4], 2); } @@ -197,103 +227,122 @@ mod tests { #[test] fn immediate_mode_multiply() { let mut program = vec![1002,4,3,4,33]; - let mut output: i32 = 0; - simulate(&mut program, 0, &mut output); + let mut input: VecDeque = VecDeque::new(); + let mut output: Vec = Vec::new(); + simulate(&mut program, &mut input, &mut output, 0); assert_eq!(program[4], 99); } #[test] fn negative_integers() { let mut program = vec![1101,100,-1,4,0]; - let mut output: i32 = 0; - simulate(&mut program, 0, &mut output); + let mut input: VecDeque = VecDeque::new(); + let mut output: Vec = Vec::new(); + simulate(&mut program, &mut input, &mut output, 0); assert_eq!(program[4], 99); } #[test] fn position_mode_input_equal_to_8() { let mut program = vec![3,9,8,9,10,9,4,9,99,-1,8]; - let mut output: i32 = 0; + let mut output: Vec = Vec::new(); let backup = program.clone(); - simulate(&mut program, 8, &mut output); - assert_eq!(output, 1); + simulate(&mut program, &mut VecDeque::from([8]), &mut output, 0); + assert_eq!(output[0], 1); + output.clear(); program = backup.clone(); - simulate(&mut program, 7, &mut output); - assert_eq!(output, 0); + simulate(&mut program, &mut VecDeque::from([7]), &mut output, 0); + assert_eq!(output[0], 0); } #[test] fn position_mode_input_less_than_8() { let mut program = vec![3,9,7,9,10,9,4,9,99,-1,8]; - let mut output: i32 = 0; + let mut output: Vec = Vec::new(); let backup = program.clone(); - simulate(&mut program, 8, &mut output); - assert_eq!(output, 0); + simulate(&mut program, &mut VecDeque::from([8]), &mut output, 0); + assert_eq!(output[0], 0); + output.clear(); program = backup.clone(); - simulate(&mut program, 7, &mut output); - assert_eq!(output, 1); + simulate(&mut program, &mut VecDeque::from([7]), &mut output, 0); + assert_eq!(output[0], 1); } #[test] fn immediate_mode_input_equal_to_8() { let mut program = vec![3,3,1108,-1,8,3,4,3,99]; - let mut output: i32 = 0; + let mut output: Vec = Vec::new(); let backup = program.clone(); - simulate(&mut program, 8, &mut output); - assert_eq!(output, 1); + simulate(&mut program, &mut VecDeque::from([8]), &mut output, 0); + assert_eq!(output[0], 1); + output.clear(); program = backup.clone(); - simulate(&mut program, 7, &mut output); - assert_eq!(output, 0); + simulate(&mut program, &mut VecDeque::from([7]), &mut output, 0); + assert_eq!(output[0], 0); } #[test] fn immediate_mode_input_less_than_8() { let mut program = vec![3,3,1107,-1,8,3,4,3,99]; - let mut output: i32 = 0; + let mut output: Vec = Vec::new(); let backup = program.clone(); - simulate(&mut program, 8, &mut output); - assert_eq!(output, 0); + simulate(&mut program, &mut VecDeque::from([8]), &mut output, 0); + assert_eq!(output[0], 0); + output.clear(); program = backup.clone(); - simulate(&mut program, 7, &mut output); - assert_eq!(output, 1); + simulate(&mut program, &mut VecDeque::from([7]), &mut output, 0); + assert_eq!(output[0], 1); } #[test] fn position_mode_input_is_non_zero() { let mut program = vec![3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9]; - let mut output: i32 = 0; + let mut output: Vec = Vec::new(); let backup = program.clone(); - simulate(&mut program, 8, &mut output); - assert_eq!(output, 1); + simulate(&mut program, &mut VecDeque::from([8]), &mut output, 0); + assert_eq!(output[0], 1); + output.clear(); program = backup.clone(); - simulate(&mut program, 0, &mut output); - assert_eq!(output, 0); + simulate(&mut program, &mut VecDeque::from([0]), &mut output, 0); + assert_eq!(output[0], 0); } #[test] fn immediate_mode_input_is_non_zero() { let mut program = vec![3,3,1105,-1,9,1101,0,0,12,4,12,99,1]; - let mut output: i32 = 0; + let mut output: Vec = Vec::new(); let backup = program.clone(); - simulate(&mut program, 8, &mut output); - assert_eq!(output, 1); + simulate(&mut program, &mut VecDeque::from([9]), &mut output, 0); + assert_eq!(output[0], 1); + output.clear(); program = backup.clone(); - simulate(&mut program, 0, &mut output); - assert_eq!(output, 0); + simulate(&mut program, &mut VecDeque::from([0]), &mut output, 0); + assert_eq!(output[0], 0); } #[test] fn multi_jump() { let mut program = vec![3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99]; - let mut output: i32 = 0; + let mut output: Vec = Vec::new(); let backup = program.clone(); - simulate(&mut program, 7, &mut output); - assert_eq!(output, 999); + simulate(&mut program, &mut VecDeque::from([7]), &mut output, 0); + assert_eq!(output[0], 999); + output.clear(); program = backup.clone(); - simulate(&mut program, 8, &mut output); - assert_eq!(output, 1000); + simulate(&mut program, &mut VecDeque::from([8]), &mut output, 0); + assert_eq!(output[0], 1000); + output.clear(); program = backup.clone(); - simulate(&mut program, 9, &mut output); - assert_eq!(output, 1001); + simulate(&mut program, &mut VecDeque::from([9]), &mut output, 0); + assert_eq!(output[0], 1001); + } + + #[test] + fn blocks_on_input() { + let mut program = vec![3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5]; + let mut output: Vec = Vec::new(); + let result = simulate(&mut program, &mut VecDeque::from([9, 0]), &mut output, 0); + assert_eq!(result.status, Status::WaitingForInput); + assert_eq!(result.instruction, 6); } }