From 7069e03132c063cae3b515b3db0110ede7202c61 Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Tue, 3 Dec 2024 17:43:36 -0500 Subject: [PATCH] Day 1 all parts complete --- crates/day_1/data/{test_case => test_input} | 0 crates/day_1/src/day_1.rs | 89 +++++++++++++++++---- crates/solver_base/src/solver_base.rs | 3 +- src/main.rs | 7 +- 4 files changed, 82 insertions(+), 17 deletions(-) rename crates/day_1/data/{test_case => test_input} (100%) diff --git a/crates/day_1/data/test_case b/crates/day_1/data/test_input similarity index 100% rename from crates/day_1/data/test_case rename to crates/day_1/data/test_input diff --git a/crates/day_1/src/day_1.rs b/crates/day_1/src/day_1.rs index a29f3bb..10067cd 100644 --- a/crates/day_1/src/day_1.rs +++ b/crates/day_1/src/day_1.rs @@ -12,14 +12,75 @@ pub struct Day1 pub final_result: i32, } +//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +// DAY 1 IMPL +//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| impl Day1 { pub fn new() -> Day1 { - Day1 { run_mode: RunMode::TestCase, do_debug_prints: false, data_a: vec![], data_b: vec![], distances: vec![], final_result: 0 } + Day1 { run_mode: RunMode::FirstCaseTest, do_debug_prints: false, data_a: vec![], data_b: vec![], distances: vec![], final_result: 0 } + } + + fn solve_first_case(self: &mut Self) -> String + { + // iterate the lists and find the differences + for i in 0..self.data_a.len() + { + self.distances.push((self.data_a[i] - self.data_b[i]).abs()); + } + + if self.do_debug_prints + { + println!("Day1 Debug: distances: {:#?}", self.distances); + } + + // iterate the list of differences and sum them + self.final_result = self.distances.iter().sum::(); + self.final_result.to_string() + } + + fn solve_second_case(self: &mut Self) -> String + { + + // need to find the largest number in either set + let largest_a = *self.data_a.iter().max().unwrap(); + let largest_b = *self.data_b.iter().max().unwrap(); + let largest = largest_a.max(largest_b) + 1; + + if self.do_debug_prints + { + println!("Day1 Debug: Largest value found: {}", largest - 1); + } + + + // count the occurance of each value in set b + let mut num_occurrences: Vec = vec![0; largest as usize]; + for i in 0..self.data_b.len() + { + num_occurrences[self.data_b[i] as usize] += 1; + } + + if self.do_debug_prints + { + println!("Day1 Debug: Number of ID occurances: {:#?}", num_occurrences); + } + + // find the similarity score + let mut sim_score = 0; + for d in self.data_a.clone() + { + sim_score += num_occurrences[d as usize] * d; + } + + self.final_result = sim_score; + self.final_result.to_string() } } +//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +// SOLVER TRAIT IMPL +//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| impl Solver for Day1 { fn print_test() @@ -41,9 +102,10 @@ impl Solver for Day1 let data_filename = match self.run_mode { - RunMode::TestCase => format!("{}/data/test_case", dir), + RunMode::FirstCaseTest => format!("{}/data/test_input", dir), RunMode::FirstCase => format!("{}/data/input", dir), - RunMode::SecondCase => format!("{}/data/input_second", dir), + RunMode::SecondCaseTest => format!("{}/data/test_input", dir), + RunMode::SecondCase => format!("{}/data/input", dir), }; @@ -78,21 +140,18 @@ impl Solver for Day1 fn solve(self: &mut Self) -> String { - // iterate the lists and find the differences - for i in 0..self.data_a.len() + match self.run_mode { - self.distances.push((self.data_a[i] - self.data_b[i]).abs()); - } + RunMode::FirstCase | RunMode::FirstCaseTest => + { + self.solve_first_case() + }, - if self.do_debug_prints - { - println!("Day1 Debug: distances: {:#?}", self.distances); + RunMode::SecondCase | RunMode::SecondCaseTest => + { + self.solve_second_case() + } } - - // iterate the list of differences and sum them - self.final_result = self.distances.iter().sum::(); - - self.final_result.to_string() } } \ No newline at end of file diff --git a/crates/solver_base/src/solver_base.rs b/crates/solver_base/src/solver_base.rs index 29382dd..e8b5321 100644 --- a/crates/solver_base/src/solver_base.rs +++ b/crates/solver_base/src/solver_base.rs @@ -3,8 +3,9 @@ #[derive(Copy, Clone, Debug, PartialEq)] pub enum RunMode { - TestCase, + FirstCaseTest, FirstCase, + SecondCaseTest, SecondCase, } diff --git a/src/main.rs b/src/main.rs index 109c15f..a7d003f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,11 @@ fn main() let mut day_1 = Day1::new(); day_1.init(RunMode::FirstCase, false); let day1_result = day_1.solve(); - println!("Day1 Final Result: {}", day1_result); + println!("Day1 Part 1 Final Result: {}", day1_result); + + let mut day_1 = Day1::new(); + day_1.init(RunMode::SecondCase, false); + let day1_result = day_1.solve(); + println!("Day1 Part 2 Final Result: {}", day1_result); }