From 176bef0c0cd22094debc970e62701ba01ea9976a Mon Sep 17 00:00:00 2001 From: Kienan Stewart Date: Sun, 5 Dec 2021 14:38:52 -0500 Subject: [PATCH] Day 2 --- .gitignore | 3 +- day2/Cargo.lock | 7 ++++ day2/Cargo.toml | 8 ++++ day2/input | 1 + day2/src/main.rs | 105 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 day2/Cargo.lock create mode 100644 day2/Cargo.toml create mode 100644 day2/input create mode 100644 day2/src/main.rs diff --git a/.gitignore b/.gitignore index 77bf7b7..2383c7d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ bin/ zig-out/ -zig-cache/ \ No newline at end of file +zig-cache/ +target/ diff --git a/day2/Cargo.lock b/day2/Cargo.lock new file mode 100644 index 0000000..63c2f60 --- /dev/null +++ b/day2/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day2" +version = "0.1.0" diff --git a/day2/Cargo.toml b/day2/Cargo.toml new file mode 100644 index 0000000..8aa34bb --- /dev/null +++ b/day2/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day2" +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/day2/input b/day2/input new file mode 100644 index 0000000..dc14d67 --- /dev/null +++ b/day2/input @@ -0,0 +1 @@ +1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,9,19,23,1,13,23,27,1,5,27,31,2,31,6,35,1,35,5,39,1,9,39,43,1,43,5,47,1,47,5,51,2,10,51,55,1,5,55,59,1,59,5,63,2,63,9,67,1,67,5,71,2,9,71,75,1,75,5,79,1,10,79,83,1,83,10,87,1,10,87,91,1,6,91,95,2,95,6,99,2,99,9,103,1,103,6,107,1,13,107,111,1,13,111,115,2,115,9,119,1,119,6,123,2,9,123,127,1,127,5,131,1,131,5,135,1,135,5,139,2,10,139,143,2,143,10,147,1,147,5,151,1,151,2,155,1,155,13,0,99,2,14,0,0 diff --git a/day2/src/main.rs b/day2/src/main.rs new file mode 100644 index 0000000..c9483f5 --- /dev/null +++ b/day2/src/main.rs @@ -0,0 +1,105 @@ +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::().unwrap()); + } + else { + program.push(val.parse::().unwrap()); + } + } + + // Create a copy of the memory to refresh from + let backup = program.clone(); + + // Modify state for part 1 + program[1] = 12; + program[2] = 2; + simulate(&mut program); + println!("[Part 1] The value at position 0 is: {}", program[0]); + + let mut noun = 0; + while noun < 100 { + let mut verb = 0; + while verb < 100 { + program = backup.clone(); + program[1] = noun; + program[2] = verb; + simulate(&mut program); + //println!("Noun {}, verb {}: {}", noun, verb, program[0]); + if program[0] == 19690720 { + println!("[Part 2] noun {}, verb {}, sum {}", noun, verb, 100*noun+verb); + break; + } + verb += 1; + } + noun += 1; + } +} + +fn simulate(v: &mut Vec) { + let mut pos = 0; + loop { + let code = v[pos]; + if code == 1 { + let i1 = v[pos+1] as usize; + let i2 = v[pos+2] as usize; + let idest = v[pos+3] as usize; + v[idest] = v[i1] + v[i2]; + pos += 4; + continue; + } + else if code == 2 { + let i1 = v[pos+1] as usize; + let i2 = v[pos+2] as usize; + let idest = v[pos+3] as usize; + v[idest] = v[i1] * v[i2]; + pos += 4; + continue; + } + else if code == 99 { + break; + } + else { + panic!("Unknown opcode"); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_op_1() { + let mut program = vec![1, 0, 0, 0, 99]; + simulate(&mut program); + assert_eq!(program[0], 2); + } + + #[test] + fn test_op_2() { + let mut program = vec![2, 3, 0, 3, 99]; + simulate(&mut program); + assert_eq!(program[3], 6); + } + + #[test] + fn test_case_3() { + let mut program = vec![2,4,4,5,99,0]; + simulate(&mut program); + 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]; + simulate(&mut program); + assert_eq!(program[0], 30); + assert_eq!(program[4], 2); + } +}