From 74d77382dd332626192db2a39045c3a04797c922 Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Tue, 10 Dec 2019 00:41:09 -0500 Subject: [PATCH] Completed day 7 part 1 --- day7/Cargo.lock | 144 ++++++++++++++++++ day7/Cargo.toml | 11 ++ day7/input/input.txt | 1 + day7/input/test1.txt | 1 + day7/input/test2.txt | 1 + day7/input/test3.txt | 1 + day7/src/intcode.rs | 351 +++++++++++++++++++++++++++++++++++++++++++ day7/src/main.rs | 136 +++++++++++++++++ 8 files changed, 646 insertions(+) create mode 100644 day7/Cargo.lock create mode 100644 day7/Cargo.toml create mode 100644 day7/input/input.txt create mode 100644 day7/input/test1.txt create mode 100644 day7/input/test2.txt create mode 100644 day7/input/test3.txt create mode 100644 day7/src/intcode.rs create mode 100644 day7/src/main.rs diff --git a/day7/Cargo.lock b/day7/Cargo.lock new file mode 100644 index 0000000..97b4ab1 --- /dev/null +++ b/day7/Cargo.lock @@ -0,0 +1,144 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "day7" +version = "0.1.0" +dependencies = [ + "num_enum 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "permutohedron 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "derivative" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num_enum" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "derivative 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "num_enum_derive 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num_enum_derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro-crate 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "permutohedron" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "proc-macro-crate" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "toml 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "proc-macro2" +version = "0.4.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "proc-macro2" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "0.6.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde" +version = "1.0.103" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "syn" +version = "0.15.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "syn" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "toml" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde 1.0.103 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-xid" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unicode-xid" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum derivative 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "942ca430eef7a3806595a6737bc388bf51adb888d3fc0dd1b50f1c170167ee3a" +"checksum num_enum 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "be601e38e20a6f3d01049d85801cb9b7a34a8da7a0da70df507bbde7735058c8" +"checksum num_enum_derive 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b59f30f6a043f2606adbd0addbf1eef6f2e28e8c4968918b63b7ff97ac0db2a7" +"checksum permutohedron 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b687ff7b5da449d39e418ad391e5e08da53ec334903ddbb921db208908fc372c" +"checksum proc-macro-crate 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "e10d4b51f154c8a7fb96fd6dad097cb74b863943ec010ac94b9fd1be8861fe1e" +"checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" +"checksum proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9c9e470a8dc4aeae2dee2f335e8f533e2d4b347e1434e5671afc49b054592f27" +"checksum quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1" +"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" +"checksum serde 1.0.103 (registry+https://github.com/rust-lang/crates.io-index)" = "1217f97ab8e8904b57dd22eb61cde455fa7446a9c1cf43966066da047c1f3702" +"checksum syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)" = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" +"checksum syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)" = "dff0acdb207ae2fe6d5976617f887eb1e35a2ba52c13c7234c790960cdad9238" +"checksum toml 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "01d1404644c8b12b16bfcffa4322403a91a451584daaaa7c28d3152e6cbc98cf" +"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" +"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" diff --git a/day7/Cargo.toml b/day7/Cargo.toml new file mode 100644 index 0000000..d973d3c --- /dev/null +++ b/day7/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "day7" +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] +num_enum = "0.4.2" +permutohedron = "0.2.4" diff --git a/day7/input/input.txt b/day7/input/input.txt new file mode 100644 index 0000000..c4d880f --- /dev/null +++ b/day7/input/input.txt @@ -0,0 +1 @@ +3,8,1001,8,10,8,105,1,0,0,21,38,63,76,93,118,199,280,361,442,99999,3,9,101,3,9,9,102,3,9,9,101,4,9,9,4,9,99,3,9,1002,9,2,9,101,5,9,9,1002,9,5,9,101,5,9,9,1002,9,4,9,4,9,99,3,9,101,2,9,9,102,3,9,9,4,9,99,3,9,101,2,9,9,102,5,9,9,1001,9,5,9,4,9,99,3,9,102,4,9,9,1001,9,3,9,1002,9,5,9,101,2,9,9,1002,9,2,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,99,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,99 diff --git a/day7/input/test1.txt b/day7/input/test1.txt new file mode 100644 index 0000000..e626457 --- /dev/null +++ b/day7/input/test1.txt @@ -0,0 +1 @@ +3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0 diff --git a/day7/input/test2.txt b/day7/input/test2.txt new file mode 100644 index 0000000..e797139 --- /dev/null +++ b/day7/input/test2.txt @@ -0,0 +1 @@ +3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0 diff --git a/day7/input/test3.txt b/day7/input/test3.txt new file mode 100644 index 0000000..2beb112 --- /dev/null +++ b/day7/input/test3.txt @@ -0,0 +1 @@ +3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0 diff --git a/day7/src/intcode.rs b/day7/src/intcode.rs new file mode 100644 index 0000000..1c3c9b6 --- /dev/null +++ b/day7/src/intcode.rs @@ -0,0 +1,351 @@ +use std::collections::VecDeque; +use std::convert::TryFrom; +use std::error::Error; +use std::fs::File; +use std::io::prelude::*; +use std::result; +use std::str::FromStr; + +use num_enum::TryFromPrimitive; + +type Result = result::Result>; + +#[derive(Debug, Clone, PartialEq)] +pub struct Intcode { + integers: Vec, +} + +#[derive(Debug, PartialEq)] +pub struct Instruction { + opcode: Opcode, + parameter_modes: Vec, +} + +impl TryFrom for Instruction { + type Error = Box; + + fn try_from(integer: i32) -> Result { + let opcode: Opcode = Opcode::try_from((integer % 100) as u8)?; + let modes_integer = integer / 100; + let mut parameter_modes = vec![]; + for parameter_index in 0..opcode.parameter_count() { + parameter_modes.push(match opcode.target_parameter_index() { + Some(target_parameter_index) + if target_parameter_index == parameter_index as usize => + { + ParameterMode::Position + } + _ => ParameterMode::try_from( + (modes_integer % (10_i32.pow(parameter_index + 1)) + / 10_i32.pow(parameter_index)) as u8, + )?, + }) + } + Ok(Instruction { + opcode, + parameter_modes, + }) + } +} + +#[derive(Debug, PartialEq, TryFromPrimitive)] +#[repr(u8)] +pub enum Opcode { + Add = 1, + Mult = 2, + Input = 3, + Output = 4, + JumpIfTrue = 5, + JumpIfFalse = 6, + LessThan = 7, + Equals = 8, + Halt = 99, +} + +impl Opcode { + pub fn parameter_count(&self) -> u32 { + match self { + Opcode::Add => 3, + Opcode::Mult => 3, + Opcode::Input => 1, + Opcode::Output => 1, + Opcode::JumpIfTrue => 2, + Opcode::JumpIfFalse => 2, + Opcode::LessThan => 3, + Opcode::Equals => 3, + Opcode::Halt => 0, + } + } + + pub fn target_parameter_index(&self) -> Option { + match self { + Opcode::Add => Some(2), + Opcode::Mult => Some(2), + Opcode::Input => Some(0), + Opcode::Output => None, + Opcode::JumpIfTrue => None, + Opcode::JumpIfFalse => None, + Opcode::LessThan => Some(2), + Opcode::Equals => Some(2), + Opcode::Halt => None, + } + } +} + +#[derive(Debug, PartialEq, TryFromPrimitive)] +#[repr(u8)] +pub enum ParameterMode { + Position = 0, + Immediate = 1, +} + +impl FromStr for Intcode { + type Err = Box; + + fn from_str(s: &str) -> Result { + let intcode_string = s.trim().to_string(); + + Ok(Intcode { + integers: intcode_string + .split(',') + .map(|code| code.parse().unwrap()) + .collect(), + }) + } +} + +impl Intcode { + fn load_parameters(&self, pointer: usize, instruction: &Instruction) -> Vec { + (0..instruction.opcode.parameter_count() as usize) + .map(|parameter_index| { + let mut integer = self.integers[pointer + parameter_index + 1]; + if let ParameterMode::Position = instruction.parameter_modes[parameter_index] { + match instruction.opcode.target_parameter_index() { + Some(target_parameter_index) + if target_parameter_index == parameter_index => {} + _ => { + integer = self.integers[integer as usize]; + } + } + } + integer + }) + .collect() + } + + pub fn execute(&mut self, inputs: &[i32]) -> Result> { + let mut pointer = 0; + let mut input_index = 0; + let mut output = vec![]; + + loop { + let instruction = Instruction::try_from(self.integers[pointer])?; + let parameters = self.load_parameters(pointer, &instruction); + let mut jump_pointer: Option = None; + + match instruction.opcode { + Opcode::Add => { + self.integers[parameters[2] as usize] = parameters[0] + parameters[1]; + } + Opcode::Mult => { + self.integers[parameters[2] as usize] = parameters[0] * parameters[1]; + } + Opcode::Input => { + self.integers[parameters[0] as usize] = inputs[input_index]; + input_index += 1; + } + Opcode::Output => { + output.push(parameters[0]); + } + Opcode::JumpIfTrue => { + if parameters[0] != 0 { + jump_pointer = Some(parameters[1] as usize); + } + } + Opcode::JumpIfFalse => { + if parameters[0] == 0 { + jump_pointer = Some(parameters[1] as usize); + } + } + Opcode::LessThan => { + if parameters[0] < parameters[1] { + self.integers[parameters[2] as usize] = 1; + } else { + self.integers[parameters[2] as usize] = 0; + } + } + Opcode::Equals => { + if parameters[0] == parameters[1] { + self.integers[parameters[2] as usize] = 1; + } else { + self.integers[parameters[2] as usize] = 0; + } + } + Opcode::Halt => { + break; + } + } + + match jump_pointer { + Some(jump_pointer) => pointer = jump_pointer, + None => pointer += 1 + instruction.opcode.parameter_count() as usize, + } + } + + Ok(output) + } +} + +pub fn read_intcode(filename: &str) -> Result { + let mut file = File::open(filename)?; + let mut intcode_string = String::new(); + file.read_to_string(&mut intcode_string)?; + + Ok(intcode_string.parse()?) +} + +#[cfg(test)] +mod tests { + use super::*; + + const TEST_INPUT: &str = "input/test1.txt"; + + #[test] + fn reads_intcode() { + assert_eq!( + read_intcode(TEST_INPUT).unwrap(), + Intcode { + integers: vec![3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0] + }, + ); + } + + #[test] + fn converts_integer_to_instruction() { + assert_eq!( + Instruction::try_from(1002).unwrap(), + Instruction { + opcode: Opcode::Mult, + parameter_modes: vec![ + ParameterMode::Position, + ParameterMode::Immediate, + ParameterMode::Position + ], + } + ); + + assert_eq!( + Instruction::try_from(101).unwrap(), + Instruction { + opcode: Opcode::Add, + parameter_modes: vec![ + ParameterMode::Immediate, + ParameterMode::Position, + ParameterMode::Position + ], + } + ); + } + + #[test] + fn executes_intcodes() { + let mut intcode = Intcode { + integers: vec![1, 0, 0, 0, 99], + }; + intcode.execute(&[0]).unwrap(); + assert_eq!(intcode.integers, vec![2, 0, 0, 0, 99]); + + let mut intcode = Intcode { + integers: vec![2, 3, 0, 3, 99], + }; + intcode.execute(&[0]).unwrap(); + assert_eq!(intcode.integers, vec![2, 3, 0, 6, 99]); + + let mut intcode = Intcode { + integers: vec![2, 4, 4, 5, 99, 0], + }; + intcode.execute(&[0]).unwrap(); + assert_eq!(intcode.integers, vec![2, 4, 4, 5, 99, 9801]); + + let mut intcode = Intcode { + integers: vec![1, 1, 1, 4, 99, 5, 6, 0, 99], + }; + intcode.execute(&[0]).unwrap(); + assert_eq!(intcode.integers, vec![30, 1, 1, 4, 2, 5, 6, 0, 99]); + + let mut intcode = Intcode { + integers: vec![1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50], + }; + intcode.execute(&[0]).unwrap(); + assert_eq!( + intcode.integers, + vec![3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50] + ); + } + + #[test] + fn less_and_equal_outputs() { + let intcode = Intcode { + integers: vec![3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8], + }; + assert_eq!(intcode.clone().execute(&[8]).unwrap(), vec![1]); + assert_eq!(intcode.clone().execute(&[0]).unwrap(), vec![0]); + + let intcode = Intcode { + integers: vec![3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8], + }; + assert_eq!(intcode.clone().execute(&[0]).unwrap(), vec![1]); + assert_eq!(intcode.clone().execute(&[9]).unwrap(), vec![0]); + + let intcode = Intcode { + integers: vec![3, 3, 1108, -1, 8, 3, 4, 3, 99], + }; + assert_eq!(intcode.clone().execute(&[8]).unwrap(), vec![1]); + assert_eq!(intcode.clone().execute(&[0]).unwrap(), vec![0]); + + let intcode = Intcode { + integers: vec![3, 3, 1107, -1, 8, 3, 4, 3, 99], + }; + assert_eq!(intcode.clone().execute(&[0]).unwrap(), vec![1]); + assert_eq!(intcode.clone().execute(&[9]).unwrap(), vec![0]); + } + + #[test] + fn jump_outputs() { + let intcode = Intcode { + integers: vec![3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9], + }; + assert_eq!(intcode.clone().execute(&[0]).unwrap(), vec![0]); + assert_eq!(intcode.clone().execute(&[1]).unwrap(), vec![1]); + + let intcode = Intcode { + integers: vec![3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1], + }; + assert_eq!(intcode.clone().execute(&[0]).unwrap(), vec![0]); + assert_eq!(intcode.clone().execute(&[1]).unwrap(), vec![1]); + } + + #[test] + fn larger_part2_intcode() { + let intcode = Intcode { + integers: 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, + ], + }; + assert_eq!(intcode.clone().execute(&[0]).unwrap(), vec![999]); + assert_eq!(intcode.clone().execute(&[8]).unwrap(), vec![1000]); + assert_eq!(intcode.clone().execute(&[9]).unwrap(), vec![1001]); + } + + #[test] + fn multiple_input_intcode() { + let intcode = Intcode { + integers: vec![ + 3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0, + ], + }; + assert_eq!(intcode.clone().execute(&[1, 1]).unwrap(), vec![11]); + } +} diff --git a/day7/src/main.rs b/day7/src/main.rs new file mode 100644 index 0000000..499a60d --- /dev/null +++ b/day7/src/main.rs @@ -0,0 +1,136 @@ +use std::error::Error; +use std::result; + +use permutohedron::Heap; + +mod intcode; + +use intcode::{read_intcode, Intcode}; + +const INPUT: &str = "input/input.txt"; + +type Result = result::Result>; + +#[derive(Debug, Clone, PartialEq)] +struct Amplifier { + intcode: Intcode, + phase_setting: i32, +} + +impl Amplifier { + fn new(intcode: Intcode, phase_setting: i32) -> Amplifier { + Amplifier { + intcode: intcode, + phase_setting: phase_setting, + } + } + + fn execute(&self, input: i32) -> Result { + let mut intcode = self.intcode.clone(); + let output = intcode.execute(&[self.phase_setting, input])?; + dbg!(&output); + Ok(output[0]) + } +} + +#[derive(Debug, Clone, PartialEq)] +struct AmplificationCircuit { + amplifiers: Vec, +} + +impl AmplificationCircuit { + fn new( + intcode: Intcode, + amplifier_count: usize, + initial_phase_settings: &[i32], + ) -> AmplificationCircuit { + AmplificationCircuit { + amplifiers: (0..amplifier_count) + .map(|index| Amplifier::new(intcode.clone(), initial_phase_settings[index])) + .collect(), + } + } + + fn set_phase_settings(&mut self, phase_settings: &[i32; 5]) { + for (index, phase_setting) in phase_settings.iter().enumerate() { + self.amplifiers[index].phase_setting = *phase_setting; + } + } + + fn execute_circuit(&self, input_signal: i32) -> Result { + let mut input = input_signal; + for amplifier in self.amplifiers.iter() { + input = amplifier.execute(input)?; + } + Ok(input) + } + + fn find_max_output(&mut self, input_signal: i32) -> Result { + let mut phase_setting: [i32; 5] = [0, 1, 2, 3, 4]; + let mut max_output = 0; + let heap = Heap::new(&mut phase_setting); + + for permutation in heap { + self.set_phase_settings(&permutation); + + let output = self.execute_circuit(input_signal)?; + if output > max_output { + max_output = output; + } + } + + Ok(max_output) + } +} + +fn solve_part1() -> Result { + let intcode = read_intcode(INPUT)?; + let mut circuit = AmplificationCircuit::new(intcode, 5, &[0, 0, 0, 0, 0]); + Ok(circuit.find_max_output(0)?) +} + +fn solve_part2() -> Result { + Ok(0) +} + +fn main() -> Result<()> { + println!("Part 1: {}", solve_part1()?); + println!("Part 2: {}", solve_part2()?); + + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + const TEST_INPUT1: &str = "input/test1.txt"; + const TEST_INPUT2: &str = "input/test2.txt"; + const TEST_INPUT3: &str = "input/test3.txt"; + + #[test] + fn executes_amplifier_circuits() { + let intcode = read_intcode(TEST_INPUT1).unwrap(); + let circuit = AmplificationCircuit::new(intcode, 5, &[4, 3, 2, 1, 0]); + assert_eq!(circuit.execute_circuit(0).unwrap(), 43210); + + let intcode = read_intcode(TEST_INPUT2).unwrap(); + let circuit = AmplificationCircuit::new(intcode, 5, &[0, 1, 2, 3, 4]); + assert_eq!(circuit.execute_circuit(0).unwrap(), 54321); + + let intcode = read_intcode(TEST_INPUT3).unwrap(); + let circuit = AmplificationCircuit::new(intcode, 5, &[1, 0, 4, 3, 2]); + assert_eq!(circuit.execute_circuit(0).unwrap(), 65210); + } + + #[test] + fn finds_max_output_of_circuits() { + let inputs = [TEST_INPUT1, TEST_INPUT2, TEST_INPUT3]; + let outputs = [43210, 54321, 65210]; + for (input, output) in inputs.iter().zip(outputs.iter()) { + let intcode = read_intcode(input).unwrap(); + let mut circuit = AmplificationCircuit::new(intcode, 5, &[0, 0, 0, 0, 0]); + assert_eq!(circuit.find_max_output(0).unwrap(), *output); + } + } +}