From c4c423871e37e65123230fbc2acd3c2b49dd68f7 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Wed, 6 Dec 2023 12:40:36 +0000 Subject: [PATCH] Day 6 Complete :) Super easy day! --- src/input/day06 | 2 ++ src/input/day06_test1 | 2 ++ src/input/day06_test2 | 2 ++ src/main.rs | 5 ++-- src/solutions/day06.rs | 60 +++++++++++++++++++++++++++++++++++++----- src/solutions/mod.rs | 4 +++ 6 files changed, 65 insertions(+), 10 deletions(-) diff --git a/src/input/day06 b/src/input/day06 index e69de29..3192d34 100644 --- a/src/input/day06 +++ b/src/input/day06 @@ -0,0 +1,2 @@ +Time: 59 70 78 78 +Distance: 430 1218 1213 1276 \ No newline at end of file diff --git a/src/input/day06_test1 b/src/input/day06_test1 index e69de29..b39f49d 100644 --- a/src/input/day06_test1 +++ b/src/input/day06_test1 @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 \ No newline at end of file diff --git a/src/input/day06_test2 b/src/input/day06_test2 index e69de29..b39f49d 100644 --- a/src/input/day06_test2 +++ b/src/input/day06_test2 @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index fc6323a..8bfc23d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ async fn main() -> Result<(), Box> { Box::new(day03::Day03 {}), Box::new(day04::Day04 {}), Box::new(day05::Day05 {}), - // Box::new(day06::Day06 {}), + Box::new(day06::Day06 {}), // Box::new(day07::Day07 {}), // Box::new(day08::Day08 {}), // Box::new(day09::Day09 {}), @@ -58,6 +58,5 @@ async fn main() -> Result<(), Box> { } println!("Ran AoC 2023 in {}ms", SystemTime::now().duration_since(start_time)?.as_millis()); - Ok(()) -} +} \ No newline at end of file diff --git a/src/solutions/day06.rs b/src/solutions/day06.rs index 69ebe63..5d6753f 100644 --- a/src/solutions/day06.rs +++ b/src/solutions/day06.rs @@ -5,16 +5,42 @@ pub struct Day06 {} impl Solution for Day06 { fn part1( &self, - _input: &mut Vec, + input: &mut Vec, ) -> Result, Box> { - Ok(Box::new("Ready")) + let times: Vec = input[0] + .split_whitespace() + .skip(1) + .map(|t| t.parse().unwrap()) + .collect(); + + let distances: Vec = input[1] + .split_whitespace() + .skip(1) + .map(|d| d.parse().unwrap()) + .collect(); + + Ok(Box::new(self.num_winning_races(×, &distances)?)) } fn part2( &self, - _input: &mut Vec, + input: &mut Vec, ) -> Result, Box> { - Ok(Box::new("Ready")) + let times = vec![input[0] + .split(":") + .nth(1) + .unwrap() + .replace(" ", "") + .parse::()?]; + + let distances = vec![input[1] + .split(":") + .nth(1) + .unwrap() + .replace(" ", "") + .parse::()?]; + + Ok(Box::new(self.num_winning_races(×, &distances)?)) } fn get_day(&self) -> u8 { @@ -22,7 +48,27 @@ impl Solution for Day06 { } } -impl Day06 {} +impl Day06 { + fn num_winning_races( + &self, + times: &Vec, + distances: &Vec, + ) -> Result> { + let mut beats = vec![]; + for (time, record) in times.iter().zip(distances.iter()) { + let mut count: u32 = 0; + for hold in 0..*time { + let dist = (hold) * (time - hold); + if dist > *record { + count += 1; + } + } + beats.push(count); + } + + Ok(beats.iter().fold(1, |acc, &b| acc * b)) + } +} /// Test from puzzle input #[cfg(test)] @@ -44,7 +90,7 @@ mod test { .unwrap() .to_string(); - assert_eq!(answer, "Ready"); + assert_eq!(answer, "288"); } #[test] @@ -61,6 +107,6 @@ mod test { .unwrap() .to_string(); - assert_eq!(answer, "Ready"); + assert_eq!(answer, "71503"); } } diff --git a/src/solutions/mod.rs b/src/solutions/mod.rs index 54fd4e1..12cccc9 100644 --- a/src/solutions/mod.rs +++ b/src/solutions/mod.rs @@ -34,6 +34,10 @@ pub trait Solution { fn run(&self) -> Result> { let start_time = SystemTime::now(); + + self.part1(get_input(self.get_day(), utils::InputType::Test1)?.as_mut())?; + self.part2(get_input(self.get_day(), utils::InputType::Test2)?.as_mut())?; + let run = Run { part1: self.part1(get_input(self.get_day(), utils::InputType::Actual)?.as_mut())?, part2: self.part2(get_input(self.get_day(), utils::InputType::Actual)?.as_mut())?,