Compare commits
3 Commits
ea9fe9fe00
...
78b8b63233
| Author | SHA1 | Date |
|---|---|---|
|
|
78b8b63233 | 1 year ago |
|
|
ed60b7ac4a | 1 year ago |
|
|
fc1a39adf5 | 1 year ago |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,6 @@
|
||||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
||||
@ -1,14 +1,98 @@
|
||||
|
||||
use solver_base::solver_base;
|
||||
use ::solver_base::solver_base::{Solver, RunMode};
|
||||
use utils::utils;
|
||||
|
||||
pub fn test_print()
|
||||
pub struct Day1
|
||||
{
|
||||
println!("DAY 1 TEST PRINT");
|
||||
solver_base::print_test();
|
||||
run_mode: RunMode,
|
||||
do_debug_prints: bool,
|
||||
data_a: Vec<i32>,
|
||||
data_b: Vec<i32>,
|
||||
distances: Vec<i32>,
|
||||
pub final_result: i32,
|
||||
}
|
||||
|
||||
let dir = utils::get_working_dir();
|
||||
let data = utils::load_data(&format!("{}/data/TESTING", dir));
|
||||
println!("DATA: {}", data);
|
||||
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 }
|
||||
}
|
||||
}
|
||||
|
||||
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::TestCase => format!("{}/data/test_case", dir),
|
||||
RunMode::FirstCase => format!("{}/data/input", dir),
|
||||
RunMode::SecondCase => format!("{}/data/input_second", 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
|
||||
{
|
||||
// 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()
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,15 +1,22 @@
|
||||
|
||||
pub trait Solver
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
pub enum RunMode
|
||||
{
|
||||
fn init(args: Vec<String>);
|
||||
fn solve();
|
||||
TestCase,
|
||||
FirstCase,
|
||||
SecondCase,
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// OLD TEST FUNCTION
|
||||
pub fn print_test()
|
||||
pub trait Solver
|
||||
{
|
||||
println!("SOLVER BASE PRINT TEST");
|
||||
|
||||
// TODO: Remove this function. Right now it just shows a "static" trait method.
|
||||
fn print_test()
|
||||
{
|
||||
println!("SOLVER BASE PRINT TEST");
|
||||
}
|
||||
|
||||
fn init(self: &mut Self, run_mode: RunMode, enable_debug_prints: bool);
|
||||
fn solve(self: &mut Self) -> String;
|
||||
}
|
||||
|
||||
@ -1,18 +1,13 @@
|
||||
|
||||
use std::env;
|
||||
|
||||
use day_1;
|
||||
use day_1::day_1::Day1;
|
||||
use solver_base::solver_base::{Solver, RunMode};
|
||||
|
||||
fn main()
|
||||
{
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
for (i,arg) in args.iter().enumerate()
|
||||
{
|
||||
println!("Arg {}: {}", i + 1, arg);
|
||||
}
|
||||
|
||||
println!("Hello, world!");
|
||||
day_1::day_1::test_print();
|
||||
let mut day_1 = Day1::new();
|
||||
day_1.init(RunMode::FirstCase, false);
|
||||
let day1_result = day_1.solve();
|
||||
println!("Day1 Final Result: {}", day1_result);
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue