01 organized and 02 base logic done
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
mod mon_02;
|
||||
mod sun_01;
|
||||
mod types;
|
||||
mod utils;
|
||||
@@ -9,8 +10,13 @@ pub fn today() {
|
||||
}
|
||||
|
||||
pub fn historian_hysteria() {
|
||||
let key = sun_01::sun_01("./assets/day_1_input").unwrap();
|
||||
// Sunday 01
|
||||
let key = sun_01::get_key("./assets/day_1_input").unwrap();
|
||||
println!("The key is: {key}");
|
||||
let similarity = sun_01::get_similarity("./assets/day_1_input");
|
||||
println!("The similarity is: {similarity}");
|
||||
|
||||
// Monday 02
|
||||
let safe_report_count = mon_02::check_reports_safety("");
|
||||
println!("There are {safe_report_count} safe reports");
|
||||
}
|
||||
|
||||
@@ -1,3 +1,59 @@
|
||||
use utils::calc_distance;
|
||||
|
||||
use super::*;
|
||||
|
||||
pub fn mon_02() {}
|
||||
pub fn check_reports_safety(input: &str) -> ReportSafety {
|
||||
let report_list: [[Report; 5]; 6] = [
|
||||
[7, 6, 4, 2, 1],
|
||||
[1, 2, 7, 8, 9],
|
||||
[9, 7, 6, 2, 1],
|
||||
[1, 3, 2, 4, 5],
|
||||
[8, 6, 4, 4, 1],
|
||||
[1, 3, 6, 7, 9],
|
||||
];
|
||||
|
||||
let mut safe_count: ReportSafety = 0;
|
||||
|
||||
// 1. All levels must be all increasing or decreasing
|
||||
// 2. All levels must change by at leat one and at most three
|
||||
|
||||
for report in report_list {
|
||||
if report.len() < 2 {
|
||||
safe_count += 1;
|
||||
break;
|
||||
}
|
||||
|
||||
let mut safe = true;
|
||||
let initial_direction: ReportDirection = get_report_direction(&report[0..=1]);
|
||||
|
||||
'report_check: for index in 1..report.len() {
|
||||
let prev = index - 1;
|
||||
let distance = calc_distance(report[index], report[prev]);
|
||||
let direction = get_report_direction(&[report[prev], report[index]]);
|
||||
|
||||
if report[index] == report[prev]
|
||||
|| distance < 1
|
||||
|| distance > 3
|
||||
|| direction != initial_direction
|
||||
{
|
||||
safe = false;
|
||||
break 'report_check;
|
||||
}
|
||||
}
|
||||
|
||||
if safe {
|
||||
safe_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
safe_count
|
||||
}
|
||||
|
||||
// FIXME: this is not a good function, since may try to access an invalid index
|
||||
fn get_report_direction(report: &[Report]) -> ReportDirection {
|
||||
if report[1] - report[0] > 0 {
|
||||
ReportDirection::Increasing
|
||||
} else {
|
||||
ReportDirection::Decreasing
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
use std::ops::Sub;
|
||||
|
||||
use super::*;
|
||||
|
||||
pub fn sun_01(input: &str) -> Result<Key, ()> {
|
||||
pub fn get_key(input: &str) -> Result<Key, ()> {
|
||||
let mut key: Id = 0;
|
||||
|
||||
let (mut list_1, mut list_2) = utils::read_id_lists(input);
|
||||
@@ -17,7 +15,7 @@ pub fn sun_01(input: &str) -> Result<Key, ()> {
|
||||
|
||||
// 2. Calc the distance between the pair elements
|
||||
for index in 0..list_1.len() {
|
||||
key += calc_distance(list_1[index], list_2[index]);
|
||||
key += utils::calc_distance(list_1[index], list_2[index]);
|
||||
}
|
||||
|
||||
Ok(key)
|
||||
@@ -41,13 +39,3 @@ pub fn get_similarity(input: &str) -> Similarity {
|
||||
|
||||
similarity
|
||||
}
|
||||
|
||||
fn calc_distance<T>(num_1: T, num_2: T) -> T
|
||||
where
|
||||
T: PartialOrd + Sub<Output = T>,
|
||||
{
|
||||
if num_1 > num_2 {
|
||||
return num_1 - num_2;
|
||||
}
|
||||
return num_2 - num_1;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,12 @@
|
||||
pub type Id = i32;
|
||||
pub type Key = Id;
|
||||
pub type Similarity = Id;
|
||||
|
||||
pub type Report = i32;
|
||||
pub type ReportSafety = u32;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum ReportDirection {
|
||||
Increasing,
|
||||
Decreasing,
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use std::fs::read_to_string;
|
||||
use std::ops::Sub;
|
||||
|
||||
use super::*;
|
||||
|
||||
@@ -15,3 +16,15 @@ pub fn read_id_lists(input: &str) -> (Vec<Id>, Vec<Id>) {
|
||||
|
||||
return (list_1, list_2);
|
||||
}
|
||||
|
||||
pub fn read_report_list(input: &str) {}
|
||||
|
||||
pub fn calc_distance<T>(num_1: T, num_2: T) -> T
|
||||
where
|
||||
T: PartialOrd + Sub<Output = T>,
|
||||
{
|
||||
if num_1 > num_2 {
|
||||
return num_1 - num_2;
|
||||
}
|
||||
return num_2 - num_1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user