This commit is contained in:
Kienan Stewart 2021-12-07 18:42:30 -05:00
parent 7ad7138e93
commit da6c5f8187
4 changed files with 343 additions and 0 deletions

7
day5/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 = "day5"
version = "0.1.0"

8
day5/Cargo.toml Normal file
View File

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

1
day5/input Normal file
View File

@ -0,0 +1 @@
3,225,1,225,6,6,1100,1,238,225,104,0,1102,72,20,224,1001,224,-1440,224,4,224,102,8,223,223,1001,224,5,224,1,224,223,223,1002,147,33,224,101,-3036,224,224,4,224,102,8,223,223,1001,224,5,224,1,224,223,223,1102,32,90,225,101,65,87,224,101,-85,224,224,4,224,1002,223,8,223,101,4,224,224,1,223,224,223,1102,33,92,225,1102,20,52,225,1101,76,89,225,1,117,122,224,101,-78,224,224,4,224,102,8,223,223,101,1,224,224,1,223,224,223,1102,54,22,225,1102,5,24,225,102,50,84,224,101,-4600,224,224,4,224,1002,223,8,223,101,3,224,224,1,223,224,223,1102,92,64,225,1101,42,83,224,101,-125,224,224,4,224,102,8,223,223,101,5,224,224,1,224,223,223,2,58,195,224,1001,224,-6840,224,4,224,102,8,223,223,101,1,224,224,1,223,224,223,1101,76,48,225,1001,92,65,224,1001,224,-154,224,4,224,1002,223,8,223,101,5,224,224,1,223,224,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,1107,677,226,224,1002,223,2,223,1005,224,329,101,1,223,223,7,677,226,224,102,2,223,223,1005,224,344,1001,223,1,223,1107,226,226,224,1002,223,2,223,1006,224,359,1001,223,1,223,8,226,226,224,1002,223,2,223,1006,224,374,101,1,223,223,108,226,226,224,102,2,223,223,1005,224,389,1001,223,1,223,1008,226,226,224,1002,223,2,223,1005,224,404,101,1,223,223,1107,226,677,224,1002,223,2,223,1006,224,419,101,1,223,223,1008,226,677,224,1002,223,2,223,1006,224,434,101,1,223,223,108,677,677,224,1002,223,2,223,1006,224,449,101,1,223,223,1108,677,226,224,102,2,223,223,1006,224,464,1001,223,1,223,107,677,677,224,102,2,223,223,1005,224,479,101,1,223,223,7,226,677,224,1002,223,2,223,1006,224,494,1001,223,1,223,7,677,677,224,102,2,223,223,1006,224,509,101,1,223,223,107,226,677,224,1002,223,2,223,1006,224,524,1001,223,1,223,1007,226,226,224,102,2,223,223,1006,224,539,1001,223,1,223,108,677,226,224,102,2,223,223,1005,224,554,101,1,223,223,1007,677,677,224,102,2,223,223,1006,224,569,101,1,223,223,8,677,226,224,102,2,223,223,1006,224,584,1001,223,1,223,1008,677,677,224,1002,223,2,223,1006,224,599,1001,223,1,223,1007,677,226,224,1002,223,2,223,1005,224,614,101,1,223,223,1108,226,677,224,1002,223,2,223,1005,224,629,101,1,223,223,1108,677,677,224,1002,223,2,223,1005,224,644,1001,223,1,223,8,226,677,224,1002,223,2,223,1006,224,659,101,1,223,223,107,226,226,224,102,2,223,223,1005,224,674,101,1,223,223,4,223,99,226

327
day5/src/main.rs Normal file
View File

@ -0,0 +1,327 @@
fn main() {
let contents = std::fs::read_to_string("input")
.expect("Failed to read file 'input'");
let it = contents.split(",");
let mut program = Vec::new();
for val in it {
if val.ends_with('\n') {
let mut s = String::from(val);
s.pop();
program.push(s.parse::<i32>().unwrap());
}
else {
program.push(val.parse::<i32>().unwrap());
}
}
// Create a copy of the memory to refresh from
let backup = program.clone();
let mut output: i32 = 0;
println!("Start part 1");
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);
}
}