Major version for int-code computer

* Programs may start at non-zero instructions
 * Input uses mutable VecDeques and consumes values as they are encountered
 * Output uses a mutable vector, and multiple outputs will be pushed on it
 * If no value is present in the input queue, the program blocks on input
 returning the status Status::WaitingForInput and the instruction to resume
 the program from.
This commit is contained in:
Kienan Stewart 2022-05-15 07:35:41 -04:00
parent f3832e6d88
commit 9411a5231d
3 changed files with 106 additions and 57 deletions

2
icc/Cargo.lock generated
View File

@ -4,4 +4,4 @@ version = 3
[[package]] [[package]]
name = "icc" name = "icc"
version = "0.1.0" version = "1.0.0"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "icc" name = "icc"
version = "0.1.0" version = "1.0.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -1,5 +1,20 @@
pub fn simulate(v: &mut Vec<i32>, input: i32, output: &mut i32) -> Option<i32> { use std::collections::VecDeque;
let mut pos = 0;
#[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<i32>, input: &mut VecDeque<i32>, output: &mut Vec<i32>, start: usize) -> Result {
let mut pos = start;
loop { loop {
let mut code = v[pos]; let mut code = v[pos];
// Opcodes are ABCDE, where the op is DE // Opcodes are ABCDE, where the op is DE
@ -57,7 +72,15 @@ pub fn simulate(v: &mut Vec<i32>, input: i32, output: &mut i32) -> Option<i32> {
else if op == 3 { else if op == 3 {
// Input // Input
let dest = v[pos+1] as usize; 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; pos += 2;
} }
else if op == 4 { else if op == 4 {
@ -68,8 +91,8 @@ pub fn simulate(v: &mut Vec<i32>, input: i32, output: &mut i32) -> Option<i32> {
else { else {
v[v[pos+1] as usize] v[v[pos+1] as usize]
}; };
println!("{}", d); println!("Output: {}", d);
*output = d; output.push(d);
pos += 2; pos += 2;
} }
else if op == 5 { else if op == 5 {
@ -155,7 +178,10 @@ pub fn simulate(v: &mut Vec<i32>, input: i32, output: &mut i32) -> Option<i32> {
panic!("Unknown opcode: {}", code); panic!("Unknown opcode: {}", code);
} }
} }
return None; return Result {
status: Status::Finished,
instruction: 0
};
} }
#[cfg(test)] #[cfg(test)]
@ -164,32 +190,36 @@ mod tests {
#[test] #[test]
fn test_op_1() { fn test_op_1() {
let mut program = vec![1, 0, 0, 0, 99]; let mut program = vec![1, 0, 0, 0, 99];
let mut output: i32 = 0; let mut input: VecDeque<i32> = VecDeque::new();
simulate(&mut program, 0, &mut output); let mut output: Vec<i32> = Vec::new();
simulate(&mut program, &mut input, &mut output, 0);
assert_eq!(program[0], 2); assert_eq!(program[0], 2);
} }
#[test] #[test]
fn test_op_2() { fn test_op_2() {
let mut program = vec![2, 3, 0, 3, 99]; let mut program = vec![2, 3, 0, 3, 99];
let mut output: i32 = 0; let mut input: VecDeque<i32> = VecDeque::new();
simulate(&mut program, 0, &mut output); let mut output: Vec<i32> = Vec::new();
simulate(&mut program, &mut input, &mut output, 0);
assert_eq!(program[3], 6); assert_eq!(program[3], 6);
} }
#[test] #[test]
fn test_case_3() { fn test_case_3() {
let mut program = vec![2,4,4,5,99,0]; let mut program = vec![2,4,4,5,99,0];
let mut output: i32 = 0; let mut input: VecDeque<i32> = VecDeque::new();
simulate(&mut program, 0, &mut output); let mut output: Vec<i32> = Vec::new();
simulate(&mut program, &mut input, &mut output, 0);
assert_eq!(program[program.len() - 1], 9801); assert_eq!(program[program.len() - 1], 9801);
} }
#[test] #[test]
fn test_case_4() { fn test_case_4() {
let mut program = vec![1,1,1,4,99,5,6,0,99]; let mut program = vec![1,1,1,4,99,5,6,0,99];
let mut output: i32 = 0; let mut input: VecDeque<i32> = VecDeque::new();
simulate(&mut program, 0, &mut output); let mut output: Vec<i32> = Vec::new();
simulate(&mut program, &mut input, &mut output, 0);
assert_eq!(program[0], 30); assert_eq!(program[0], 30);
assert_eq!(program[4], 2); assert_eq!(program[4], 2);
} }
@ -197,103 +227,122 @@ mod tests {
#[test] #[test]
fn immediate_mode_multiply() { fn immediate_mode_multiply() {
let mut program = vec![1002,4,3,4,33]; let mut program = vec![1002,4,3,4,33];
let mut output: i32 = 0; let mut input: VecDeque<i32> = VecDeque::new();
simulate(&mut program, 0, &mut output); let mut output: Vec<i32> = Vec::new();
simulate(&mut program, &mut input, &mut output, 0);
assert_eq!(program[4], 99); assert_eq!(program[4], 99);
} }
#[test] #[test]
fn negative_integers() { fn negative_integers() {
let mut program = vec![1101,100,-1,4,0]; let mut program = vec![1101,100,-1,4,0];
let mut output: i32 = 0; let mut input: VecDeque<i32> = VecDeque::new();
simulate(&mut program, 0, &mut output); let mut output: Vec<i32> = Vec::new();
simulate(&mut program, &mut input, &mut output, 0);
assert_eq!(program[4], 99); assert_eq!(program[4], 99);
} }
#[test] #[test]
fn position_mode_input_equal_to_8() { fn position_mode_input_equal_to_8() {
let mut program = vec![3,9,8,9,10,9,4,9,99,-1,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<i32> = Vec::new();
let backup = program.clone(); let backup = program.clone();
simulate(&mut program, 8, &mut output); simulate(&mut program, &mut VecDeque::from([8]), &mut output, 0);
assert_eq!(output, 1); assert_eq!(output[0], 1);
output.clear();
program = backup.clone(); program = backup.clone();
simulate(&mut program, 7, &mut output); simulate(&mut program, &mut VecDeque::from([7]), &mut output, 0);
assert_eq!(output, 0); assert_eq!(output[0], 0);
} }
#[test] #[test]
fn position_mode_input_less_than_8() { fn position_mode_input_less_than_8() {
let mut program = vec![3,9,7,9,10,9,4,9,99,-1,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<i32> = Vec::new();
let backup = program.clone(); let backup = program.clone();
simulate(&mut program, 8, &mut output); simulate(&mut program, &mut VecDeque::from([8]), &mut output, 0);
assert_eq!(output, 0); assert_eq!(output[0], 0);
output.clear();
program = backup.clone(); program = backup.clone();
simulate(&mut program, 7, &mut output); simulate(&mut program, &mut VecDeque::from([7]), &mut output, 0);
assert_eq!(output, 1); assert_eq!(output[0], 1);
} }
#[test] #[test]
fn immediate_mode_input_equal_to_8() { fn immediate_mode_input_equal_to_8() {
let mut program = vec![3,3,1108,-1,8,3,4,3,99]; let mut program = vec![3,3,1108,-1,8,3,4,3,99];
let mut output: i32 = 0; let mut output: Vec<i32> = Vec::new();
let backup = program.clone(); let backup = program.clone();
simulate(&mut program, 8, &mut output); simulate(&mut program, &mut VecDeque::from([8]), &mut output, 0);
assert_eq!(output, 1); assert_eq!(output[0], 1);
output.clear();
program = backup.clone(); program = backup.clone();
simulate(&mut program, 7, &mut output); simulate(&mut program, &mut VecDeque::from([7]), &mut output, 0);
assert_eq!(output, 0); assert_eq!(output[0], 0);
} }
#[test] #[test]
fn immediate_mode_input_less_than_8() { fn immediate_mode_input_less_than_8() {
let mut program = vec![3,3,1107,-1,8,3,4,3,99]; let mut program = vec![3,3,1107,-1,8,3,4,3,99];
let mut output: i32 = 0; let mut output: Vec<i32> = Vec::new();
let backup = program.clone(); let backup = program.clone();
simulate(&mut program, 8, &mut output); simulate(&mut program, &mut VecDeque::from([8]), &mut output, 0);
assert_eq!(output, 0); assert_eq!(output[0], 0);
output.clear();
program = backup.clone(); program = backup.clone();
simulate(&mut program, 7, &mut output); simulate(&mut program, &mut VecDeque::from([7]), &mut output, 0);
assert_eq!(output, 1); assert_eq!(output[0], 1);
} }
#[test] #[test]
fn position_mode_input_is_non_zero() { 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 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<i32> = Vec::new();
let backup = program.clone(); let backup = program.clone();
simulate(&mut program, 8, &mut output); simulate(&mut program, &mut VecDeque::from([8]), &mut output, 0);
assert_eq!(output, 1); assert_eq!(output[0], 1);
output.clear();
program = backup.clone(); program = backup.clone();
simulate(&mut program, 0, &mut output); simulate(&mut program, &mut VecDeque::from([0]), &mut output, 0);
assert_eq!(output, 0); assert_eq!(output[0], 0);
} }
#[test] #[test]
fn immediate_mode_input_is_non_zero() { 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 program = vec![3,3,1105,-1,9,1101,0,0,12,4,12,99,1];
let mut output: i32 = 0; let mut output: Vec<i32> = Vec::new();
let backup = program.clone(); let backup = program.clone();
simulate(&mut program, 8, &mut output); simulate(&mut program, &mut VecDeque::from([9]), &mut output, 0);
assert_eq!(output, 1); assert_eq!(output[0], 1);
output.clear();
program = backup.clone(); program = backup.clone();
simulate(&mut program, 0, &mut output); simulate(&mut program, &mut VecDeque::from([0]), &mut output, 0);
assert_eq!(output, 0); assert_eq!(output[0], 0);
} }
#[test] #[test]
fn multi_jump() { 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 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<i32> = Vec::new();
let backup = program.clone(); let backup = program.clone();
simulate(&mut program, 7, &mut output); simulate(&mut program, &mut VecDeque::from([7]), &mut output, 0);
assert_eq!(output, 999); assert_eq!(output[0], 999);
output.clear();
program = backup.clone(); program = backup.clone();
simulate(&mut program, 8, &mut output); simulate(&mut program, &mut VecDeque::from([8]), &mut output, 0);
assert_eq!(output, 1000); assert_eq!(output[0], 1000);
output.clear();
program = backup.clone(); program = backup.clone();
simulate(&mut program, 9, &mut output); simulate(&mut program, &mut VecDeque::from([9]), &mut output, 0);
assert_eq!(output, 1001); 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<i32> = 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);
} }
} }