Day 1 all parts complete

master
Joey Pollack 1 year ago
parent 78b8b63233
commit 7069e03132

@ -12,14 +12,75 @@ pub struct Day1
pub final_result: i32, pub final_result: i32,
} }
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// DAY 1 IMPL
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
impl Day1 impl Day1
{ {
pub fn new() -> 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::<i32>();
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<i32> = 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 impl Solver for Day1
{ {
fn print_test() fn print_test()
@ -41,9 +102,10 @@ impl Solver for Day1
let data_filename = let data_filename =
match self.run_mode 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::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 fn solve(self: &mut Self) -> String
{ {
// iterate the lists and find the differences match self.run_mode
for i in 0..self.data_a.len()
{ {
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 RunMode::SecondCase | RunMode::SecondCaseTest =>
{ {
println!("Day1 Debug: distances: {:#?}", self.distances); self.solve_second_case()
}
} }
// iterate the list of differences and sum them
self.final_result = self.distances.iter().sum::<i32>();
self.final_result.to_string()
} }
} }

@ -3,8 +3,9 @@
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
pub enum RunMode pub enum RunMode
{ {
TestCase, FirstCaseTest,
FirstCase, FirstCase,
SecondCaseTest,
SecondCase, SecondCase,
} }

@ -8,6 +8,11 @@ fn main()
let mut day_1 = Day1::new(); let mut day_1 = Day1::new();
day_1.init(RunMode::FirstCase, false); day_1.init(RunMode::FirstCase, false);
let day1_result = day_1.solve(); 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);
} }

Loading…
Cancel
Save