From a28411f50335383245a7b8553d399a2be0d37de7 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Thu, 14 Dec 2023 14:04:53 -0500 Subject: [PATCH] Complete 2023 day 6 --- 2023/day6/src/main.rs | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/2023/day6/src/main.rs b/2023/day6/src/main.rs index 470d5cc..38e9879 100644 --- a/2023/day6/src/main.rs +++ b/2023/day6/src/main.rs @@ -65,7 +65,8 @@ impl Races { .nth(1) .unwrap() .replace(' ', "") - .parse::().unwrap(); + .parse::() + .unwrap(); let distance = lines .next() @@ -73,10 +74,11 @@ impl Races { .nth(1) .unwrap() .replace(' ', "") - .parse::().unwrap(); + .parse::() + .unwrap(); Races { - records: Vec::from([RaceRecord { time, distance}]) + records: Vec::from([RaceRecord { time, distance }]), } } @@ -87,6 +89,10 @@ impl Races { .collect() } + fn calculate_win_count(&self) -> usize { + winning_possibilities(self.records[0]).len() + } + fn calculate_possible_win_product(&self) -> usize { self.calculate_winning_counts() .into_iter() @@ -113,11 +119,24 @@ fn winning_possibilities(race: RaceRecord) -> Vec { fn part_one() { let records = Races::parse(FILE_STR); - println!("Part 1 Product of winning possibilitiesL {}", records.calculate_possible_win_product()); + println!( + "Part 1 Product of winning possibilities: {}", + records.calculate_possible_win_product() + ); +} + +fn part_two() { + let record = Races::parse_single(FILE_STR); + println!("{:#?}", record); + println!( + "Part 2 winning possibilities: {}", + record.calculate_win_count() + ); } fn main() { part_one(); + part_two(); } #[cfg(test)] @@ -137,4 +156,10 @@ mod tests { let races = Races::parse(EXAMPLE_FILE_STR); assert_eq!(288, races.calculate_possible_win_product()); } + + #[test] + fn test_calculate_win_count() { + let races = Races::parse_single(EXAMPLE_FILE_STR); + assert_eq!(71503, races.calculate_win_count()); + } }