You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
3.6 KiB
Rust

use ::solver_base::solver_base::{Solver, RunMode};
use utils::utils;
pub struct Day1
{
run_mode: RunMode,
do_debug_prints: bool,
data_a: Vec<i32>,
data_b: Vec<i32>,
distances: Vec<i32>,
pub final_result: i32,
}
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// DAY 1 IMPL
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
impl Day1
{
pub fn new() -> Day1
{
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()
{
println!("DAY 1 TEST PRINT");
// solver_base::print_test();
let dir = utils::get_working_dir();
let data = utils::load_data(&format!("{}/data/TESTING", dir));
println!("DATA: {}", data);
}
fn init(self: &mut Self, run_mode: RunMode, enable_debug_prints: bool)
{
self.run_mode = run_mode;
self.do_debug_prints = enable_debug_prints;
let dir = utils::get_working_dir();
let data_filename =
match self.run_mode
{
RunMode::FirstCaseTest => format!("{}/data/test_input", dir),
RunMode::FirstCase => format!("{}/data/input", dir),
RunMode::SecondCaseTest => format!("{}/data/test_input", dir),
RunMode::SecondCase => format!("{}/data/input", dir),
};
let data = utils::load_data(&data_filename);
// Split the data and convert to i32 values
let data: Vec<i32> = data.split_ascii_whitespace().map(|x| x.parse::<i32>().unwrap()).collect();
// Split by column
for (i, d) in data.iter().enumerate()
{
// if i is even
if i % 2 < 1
{
self.data_a.push(*d)
}
else
{
self.data_b.push(*d);
}
}
self.data_a.sort();
self.data_b.sort();
if self.do_debug_prints
{
println!("Day1 Debug: Input data processed:\n\tdata_a: {:#?}\n\tdata_b: {:#?}", self.data_a, self.data_b);
}
}
fn solve(self: &mut Self) -> String
{
match self.run_mode
{
RunMode::FirstCase | RunMode::FirstCaseTest =>
{
self.solve_first_case()
},
RunMode::SecondCase | RunMode::SecondCaseTest =>
{
self.solve_second_case()
}
}
}
}