Move intcode computer to separate crate

This commit is contained in:
Kienan Stewart 2022-05-10 07:04:24 -04:00
parent 163547667c
commit f3832e6d88
6 changed files with 327 additions and 303 deletions

7
day5/Cargo.lock generated
View File

@ -4,4 +4,11 @@ version = 3
[[package]]
name = "day5"
version = "0.2.0"
dependencies = [
"icc",
]
[[package]]
name = "icc"
version = "0.1.0"

View File

@ -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"}

View File

@ -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<i32>, input: i32, output: &mut i32) -> Option<i32> {
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);
}

7
icc/Cargo.lock generated Normal file
View File

@ -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"

8
icc/Cargo.toml Normal file
View File

@ -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]

299
icc/src/lib.rs Normal file
View File

@ -0,0 +1,299 @@
pub fn simulate(v: &mut Vec<i32>, input: i32, output: &mut i32) -> Option<i32> {
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);
}
}