From f3832e6d885c7b88cbb5fb55f256130a69fe1baf Mon Sep 17 00:00:00 2001 From: Kienan Stewart Date: Tue, 10 May 2022 07:04:24 -0400 Subject: [PATCH] Move intcode computer to separate crate --- day5/Cargo.lock | 7 ++ day5/Cargo.toml | 3 +- day5/src/main.rs | 306 +---------------------------------------------- icc/Cargo.lock | 7 ++ icc/Cargo.toml | 8 ++ icc/src/lib.rs | 299 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 327 insertions(+), 303 deletions(-) create mode 100644 icc/Cargo.lock create mode 100644 icc/Cargo.toml create mode 100644 icc/src/lib.rs diff --git a/day5/Cargo.lock b/day5/Cargo.lock index 7a62bb0..72ffd9e 100644 --- a/day5/Cargo.lock +++ b/day5/Cargo.lock @@ -4,4 +4,11 @@ version = 3 [[package]] name = "day5" +version = "0.2.0" +dependencies = [ + "icc", +] + +[[package]] +name = "icc" version = "0.1.0" diff --git a/day5/Cargo.toml b/day5/Cargo.toml index f9b62bf..d09179e 100644 --- a/day5/Cargo.toml +++ b/day5/Cargo.toml @@ -1,8 +1,9 @@ [package] name = "day5" -version = "0.1.0" +version = "0.2.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +icc = { path = "../icc", version = "0.1.0"} \ No newline at end of file diff --git a/day5/src/main.rs b/day5/src/main.rs index 366f4f6..c84646a 100644 --- a/day5/src/main.rs +++ b/day5/src/main.rs @@ -1,3 +1,5 @@ +use icc; + fn main() { let contents = std::fs::read_to_string("input") .expect("Failed to read file 'input'"); @@ -19,309 +21,9 @@ fn main() { let mut output: i32 = 0; println!("Start part 1"); - simulate(&mut program, 1, &mut output); + icc::simulate(&mut program, 1, &mut output); program = backup.clone(); println!("\nStart part 2"); - simulate(&mut program, 5, &mut output); -} - -fn simulate(v: &mut Vec, input: i32, output: &mut i32) -> Option { - let mut pos = 0; - loop { - let mut code = v[pos]; - // Opcodes are ABCDE, where the op is DE - // the modes for params 1, 2, and 3 as C, B, A - // respectively. - let op = code % 100; - //println!("pos {}, code {}, op {}", pos, code, op); - let param_3_mode = code / 10000; - if param_3_mode == 1 { - code -= 10000; - } - let param_2_mode = code / 1000; - if param_2_mode == 1 { - code -= 1000; - } - let param_1_mode = code / 100; - if param_1_mode == 1 { - code -= 100; - } - if op == 1 { - // Add d1 + d2 -> dest - let d1: i32 = if param_1_mode == 1 { - v[pos+1] - } else { - v[v[pos+1] as usize] - }; - let d2: i32 = if param_2_mode == 1 { - v[pos+2] - } else { - v[v[pos+2] as usize] - }; - let dest = v[pos+3] as usize; - v[dest] = d1 + d2; - pos += 4; - continue; - } - else if op == 2 { - // Mult d1 * d2 -> dest - let d1: i32 = if param_1_mode == 1 { - v[pos+1] - } else { - v[v[pos+1] as usize] - }; - let d2: i32 = if param_2_mode == 1 { - v[pos+2] - } - else { - v[v[pos+2] as usize] - }; - let dest = v[pos+3] as usize; - v[dest] = d1 * d2; - pos += 4; - continue; - } - else if op == 3 { - // Input - let dest = v[pos+1] as usize; - v[dest] = input; - pos += 2; - } - else if op == 4 { - // Output - let d = if param_1_mode == 1 { - v[pos+1] - } - else { - v[v[pos+1] as usize] - }; - println!("{}", d); - *output = d; - pos += 2; - } - else if op == 5 { - // Jump if true - let d1 = if param_1_mode == 1 { - v[pos+1] as usize - } - else { - v[v[pos+1] as usize] as usize - }; - let d2 = if param_2_mode == 1 { - v[pos+2] as usize - } - else { - v[v[pos+2] as usize] as usize - }; - if d1 != 0 { - pos = d2; - } - else { - pos += 3; - } - } - else if op == 6 { - // Jump if false - let d1 = if param_1_mode == 1 { - v[pos+1] as usize - } - else { - v[v[pos+1] as usize] as usize - }; - let d2 = if param_2_mode == 1 { - v[pos+2] as usize - } - else { - v[v[pos+2] as usize] as usize - }; - if d1 == 0 { - pos = d2; - } - else { - pos += 3; - } - } - else if op == 7 { - // Less than - let d1: i32 = if param_1_mode == 1 { - v[pos+1] - } else { - v[v[pos+1] as usize] - }; - let d2: i32 = if param_2_mode == 1 { - v[pos+2] - } - else { - v[v[pos+2] as usize] - }; - let dest = v[pos+3] as usize; - v[dest] = if d1 < d2 { 1 } else { 0 }; - pos += 4; - } - else if op == 8 { - // equals - let d1: i32 = if param_1_mode == 1 { - v[pos+1] - } else { - v[v[pos+1] as usize] - }; - let d2: i32 = if param_2_mode == 1 { - v[pos+2] - } - else { - v[v[pos+2] as usize] - }; - let dest = v[pos+3] as usize; - v[dest] = if d1 == d2 { 1 } else { 0 }; - pos += 4; - } - else if op == 99 { - break; - } - else { - panic!("Unknown opcode: {}", code); - } - } - return None; -} - -#[cfg(test)] -mod tests { - use super::*; - #[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); - 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); - 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); - 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); - assert_eq!(program[0], 30); - assert_eq!(program[4], 2); - } - - #[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); - 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); - 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 backup = program.clone(); - simulate(&mut program, 8, &mut output); - assert_eq!(output, 1); - program = backup.clone(); - simulate(&mut program, 7, &mut output); - assert_eq!(output, 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 backup = program.clone(); - simulate(&mut program, 8, &mut output); - assert_eq!(output, 0); - program = backup.clone(); - simulate(&mut program, 7, &mut output); - assert_eq!(output, 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 backup = program.clone(); - simulate(&mut program, 8, &mut output); - assert_eq!(output, 1); - program = backup.clone(); - simulate(&mut program, 7, &mut output); - assert_eq!(output, 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 backup = program.clone(); - simulate(&mut program, 8, &mut output); - assert_eq!(output, 0); - program = backup.clone(); - simulate(&mut program, 7, &mut output); - assert_eq!(output, 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 backup = program.clone(); - simulate(&mut program, 8, &mut output); - assert_eq!(output, 1); - program = backup.clone(); - simulate(&mut program, 0, &mut output); - assert_eq!(output, 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 backup = program.clone(); - simulate(&mut program, 8, &mut output); - assert_eq!(output, 1); - program = backup.clone(); - simulate(&mut program, 0, &mut output); - assert_eq!(output, 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 backup = program.clone(); - simulate(&mut program, 7, &mut output); - assert_eq!(output, 999); - program = backup.clone(); - simulate(&mut program, 8, &mut output); - assert_eq!(output, 1000); - program = backup.clone(); - simulate(&mut program, 9, &mut output); - assert_eq!(output, 1001); - } + icc::simulate(&mut program, 5, &mut output); } diff --git a/icc/Cargo.lock b/icc/Cargo.lock new file mode 100644 index 0000000..277f5fb --- /dev/null +++ b/icc/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "icc" +version = "0.1.0" diff --git a/icc/Cargo.toml b/icc/Cargo.toml new file mode 100644 index 0000000..f2620c8 --- /dev/null +++ b/icc/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "icc" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/icc/src/lib.rs b/icc/src/lib.rs new file mode 100644 index 0000000..d294236 --- /dev/null +++ b/icc/src/lib.rs @@ -0,0 +1,299 @@ +pub fn simulate(v: &mut Vec, input: i32, output: &mut i32) -> Option { + let mut pos = 0; + loop { + let mut code = v[pos]; + // Opcodes are ABCDE, where the op is DE + // the modes for params 1, 2, and 3 as C, B, A + // respectively. + let op = code % 100; + //println!("pos {}, code {}, op {}", pos, code, op); + let param_3_mode = code / 10000; + if param_3_mode == 1 { + code -= 10000; + } + let param_2_mode = code / 1000; + if param_2_mode == 1 { + code -= 1000; + } + let param_1_mode = code / 100; + if param_1_mode == 1 { + code -= 100; + } + if op == 1 { + // Add d1 + d2 -> dest + let d1: i32 = if param_1_mode == 1 { + v[pos+1] + } else { + v[v[pos+1] as usize] + }; + let d2: i32 = if param_2_mode == 1 { + v[pos+2] + } else { + v[v[pos+2] as usize] + }; + let dest = v[pos+3] as usize; + v[dest] = d1 + d2; + pos += 4; + continue; + } + else if op == 2 { + // Mult d1 * d2 -> dest + let d1: i32 = if param_1_mode == 1 { + v[pos+1] + } else { + v[v[pos+1] as usize] + }; + let d2: i32 = if param_2_mode == 1 { + v[pos+2] + } + else { + v[v[pos+2] as usize] + }; + let dest = v[pos+3] as usize; + v[dest] = d1 * d2; + pos += 4; + continue; + } + else if op == 3 { + // Input + let dest = v[pos+1] as usize; + v[dest] = input; + pos += 2; + } + else if op == 4 { + // Output + let d = if param_1_mode == 1 { + v[pos+1] + } + else { + v[v[pos+1] as usize] + }; + println!("{}", d); + *output = d; + pos += 2; + } + else if op == 5 { + // Jump if true + let d1 = if param_1_mode == 1 { + v[pos+1] as usize + } + else { + v[v[pos+1] as usize] as usize + }; + let d2 = if param_2_mode == 1 { + v[pos+2] as usize + } + else { + v[v[pos+2] as usize] as usize + }; + if d1 != 0 { + pos = d2; + } + else { + pos += 3; + } + } + else if op == 6 { + // Jump if false + let d1 = if param_1_mode == 1 { + v[pos+1] as usize + } + else { + v[v[pos+1] as usize] as usize + }; + let d2 = if param_2_mode == 1 { + v[pos+2] as usize + } + else { + v[v[pos+2] as usize] as usize + }; + if d1 == 0 { + pos = d2; + } + else { + pos += 3; + } + } + else if op == 7 { + // Less than + let d1: i32 = if param_1_mode == 1 { + v[pos+1] + } else { + v[v[pos+1] as usize] + }; + let d2: i32 = if param_2_mode == 1 { + v[pos+2] + } + else { + v[v[pos+2] as usize] + }; + let dest = v[pos+3] as usize; + v[dest] = if d1 < d2 { 1 } else { 0 }; + pos += 4; + } + else if op == 8 { + // equals + let d1: i32 = if param_1_mode == 1 { + v[pos+1] + } else { + v[v[pos+1] as usize] + }; + let d2: i32 = if param_2_mode == 1 { + v[pos+2] + } + else { + v[v[pos+2] as usize] + }; + let dest = v[pos+3] as usize; + v[dest] = if d1 == d2 { 1 } else { 0 }; + pos += 4; + } + else if op == 99 { + break; + } + else { + panic!("Unknown opcode: {}", code); + } + } + return None; +} + +#[cfg(test)] +mod tests { + use super::*; + #[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); + 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); + 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); + 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); + assert_eq!(program[0], 30); + assert_eq!(program[4], 2); + } + + #[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); + 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); + 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 backup = program.clone(); + simulate(&mut program, 8, &mut output); + assert_eq!(output, 1); + program = backup.clone(); + simulate(&mut program, 7, &mut output); + assert_eq!(output, 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 backup = program.clone(); + simulate(&mut program, 8, &mut output); + assert_eq!(output, 0); + program = backup.clone(); + simulate(&mut program, 7, &mut output); + assert_eq!(output, 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 backup = program.clone(); + simulate(&mut program, 8, &mut output); + assert_eq!(output, 1); + program = backup.clone(); + simulate(&mut program, 7, &mut output); + assert_eq!(output, 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 backup = program.clone(); + simulate(&mut program, 8, &mut output); + assert_eq!(output, 0); + program = backup.clone(); + simulate(&mut program, 7, &mut output); + assert_eq!(output, 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 backup = program.clone(); + simulate(&mut program, 8, &mut output); + assert_eq!(output, 1); + program = backup.clone(); + simulate(&mut program, 0, &mut output); + assert_eq!(output, 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 backup = program.clone(); + simulate(&mut program, 8, &mut output); + assert_eq!(output, 1); + program = backup.clone(); + simulate(&mut program, 0, &mut output); + assert_eq!(output, 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 backup = program.clone(); + simulate(&mut program, 7, &mut output); + assert_eq!(output, 999); + program = backup.clone(); + simulate(&mut program, 8, &mut output); + assert_eq!(output, 1000); + program = backup.clone(); + simulate(&mut program, 9, &mut output); + assert_eq!(output, 1001); + } +}