From 42336635822b43e7b80b7dce1d12d04c53bc27c2 Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Tue, 3 Dec 2019 23:35:00 -0500 Subject: [PATCH] Completed day 2 --- day1/.gitignore => .gitignore | 2 +- day2/Cargo.lock | 6 ++ day2/Cargo.toml | 9 +++ day2/input/input.txt | 1 + day2/input/test.txt | 1 + day2/src/main.rs | 112 ++++++++++++++++++++++++++++++++++ 6 files changed, 130 insertions(+), 1 deletion(-) rename day1/.gitignore => .gitignore (52%) create mode 100644 day2/Cargo.lock create mode 100644 day2/Cargo.toml create mode 100644 day2/input/input.txt create mode 100644 day2/input/test.txt create mode 100644 day2/src/main.rs diff --git a/day1/.gitignore b/.gitignore similarity index 52% rename from day1/.gitignore rename to .gitignore index 53eaa21..c8f7745 100644 --- a/day1/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -/target +**/target **/*.rs.bk diff --git a/day2/Cargo.lock b/day2/Cargo.lock new file mode 100644 index 0000000..40a1175 --- /dev/null +++ b/day2/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "day2" +version = "0.1.0" + diff --git a/day2/Cargo.toml b/day2/Cargo.toml new file mode 100644 index 0000000..f264fb9 --- /dev/null +++ b/day2/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day2" +version = "0.1.0" +authors = ["Tyler Hallada "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day2/input/input.txt b/day2/input/input.txt new file mode 100644 index 0000000..45783ba --- /dev/null +++ b/day2/input/input.txt @@ -0,0 +1 @@ +1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,6,19,23,2,23,6,27,2,6,27,31,2,13,31,35,1,10,35,39,2,39,13,43,1,43,13,47,1,6,47,51,1,10,51,55,2,55,6,59,1,5,59,63,2,9,63,67,1,6,67,71,2,9,71,75,1,6,75,79,2,79,13,83,1,83,10,87,1,13,87,91,1,91,10,95,2,9,95,99,1,5,99,103,2,10,103,107,1,107,2,111,1,111,5,0,99,2,14,0,0 diff --git a/day2/input/test.txt b/day2/input/test.txt new file mode 100644 index 0000000..2912131 --- /dev/null +++ b/day2/input/test.txt @@ -0,0 +1 @@ +1,9,10,3,2,3,11,0,99,30,40,50 diff --git a/day2/src/main.rs b/day2/src/main.rs new file mode 100644 index 0000000..db1dafb --- /dev/null +++ b/day2/src/main.rs @@ -0,0 +1,112 @@ +use std::fs::File; +use std::io::{self, prelude::*}; + +const INPUT: &str = "input/input.txt"; + +fn read_intcode(filename: &str) -> io::Result> { + let mut file = File::open(filename)?; + let mut intcode_string = String::new(); + file.read_to_string(&mut intcode_string)?; + let intcode_string = intcode_string.trim().to_string(); + + Ok(intcode_string + .split(',') + .map(|code| code.parse().unwrap()) + .collect()) +} + +fn run_intcode(intcode: &mut Vec) { + let mut pointer = 0; + + loop { + match intcode[pointer] { + 1 => { + let a = intcode[intcode[pointer + 1] as usize]; + let b = intcode[intcode[pointer + 2] as usize]; + let target = intcode[pointer + 3] as usize; + intcode[target] = a + b; + } + 2 => { + let a = intcode[intcode[pointer + 1] as usize]; + let b = intcode[intcode[pointer + 2] as usize]; + let target = intcode[pointer + 3] as usize; + intcode[target] = a * b; + } + 99 => { + break; + } + invalid => panic!("Invalid opcode: {}", invalid), + } + + pointer += 4; + } +} + +fn solve_part1() -> io::Result { + let mut intcode = read_intcode(INPUT)?; + intcode[1] = 12; + intcode[2] = 2; + run_intcode(&mut intcode); + Ok(intcode[0]) +} + +fn solve_part2() -> io::Result { + let original_intcode = read_intcode(INPUT)?; + for noun in 0..99 { + for verb in 0..99 { + let mut intcode = original_intcode.clone(); + intcode[1] = noun; + intcode[2] = verb; + run_intcode(&mut intcode); + if intcode[0] == 19690720 { + return Ok(100 * noun + verb) + } + } + } + panic!("Could not find a noun and verb that produced the target value") +} + +fn main() -> io::Result<()> { + println!("Part 1: {}", solve_part1()?); + println!("Part 2: {}", solve_part2()?); + + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + const TEST_INPUT: &str = "input/test.txt"; + + #[test] + fn reads_intcode() { + assert_eq!( + read_intcode(TEST_INPUT).unwrap(), + vec![1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50] + ); + } + + #[test] + fn runs_intcodes() { + let mut intcode = vec![1, 0, 0, 0, 99]; + run_intcode(&mut intcode); + assert_eq!(intcode, vec![2, 0, 0, 0, 99]); + + let mut intcode = vec![2, 3, 0, 3, 99]; + run_intcode(&mut intcode); + assert_eq!(intcode, vec![2, 3, 0, 6, 99]); + + let mut intcode = vec![2, 4, 4, 5, 99, 0]; + run_intcode(&mut intcode); + assert_eq!(intcode, vec![2, 4, 4, 5, 99, 9801]); + + let mut intcode = vec![1, 1, 1, 4, 99, 5, 6, 0, 99]; + run_intcode(&mut intcode); + assert_eq!(intcode, vec![30, 1, 1, 4, 2, 5, 6, 0, 99]); + + let mut intcode = vec![1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50]; + run_intcode(&mut intcode); + assert_eq!(intcode, vec![3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50]); + } +}