|
|
|
|
@ -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::<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
|
|
|
|
|
{
|
|
|
|
|
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::<i32>();
|
|
|
|
|
|
|
|
|
|
self.final_result.to_string()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|