From f30a2ff86fbf67c6da755e6cb12961cc510d56c3 Mon Sep 17 00:00:00 2001 From: Daniel Heras Quesada Date: Tue, 3 Dec 2024 16:20:59 +0100 Subject: [PATCH] 01 organized and 02 base logic done --- src/advent_of_code/mod.rs | 8 ++++- src/advent_of_code/mon_02.rs | 58 +++++++++++++++++++++++++++++++++++- src/advent_of_code/sun_01.rs | 16 ++-------- src/advent_of_code/types.rs | 9 ++++++ src/advent_of_code/utils.rs | 13 ++++++++ 5 files changed, 88 insertions(+), 16 deletions(-) diff --git a/src/advent_of_code/mod.rs b/src/advent_of_code/mod.rs index 675d951..bfd514f 100644 --- a/src/advent_of_code/mod.rs +++ b/src/advent_of_code/mod.rs @@ -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"); } diff --git a/src/advent_of_code/mon_02.rs b/src/advent_of_code/mon_02.rs index e1990a5..ad17122 100644 --- a/src/advent_of_code/mon_02.rs +++ b/src/advent_of_code/mon_02.rs @@ -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 + } +} diff --git a/src/advent_of_code/sun_01.rs b/src/advent_of_code/sun_01.rs index f528d87..7bd755e 100644 --- a/src/advent_of_code/sun_01.rs +++ b/src/advent_of_code/sun_01.rs @@ -1,8 +1,6 @@ -use std::ops::Sub; - use super::*; -pub fn sun_01(input: &str) -> Result { +pub fn get_key(input: &str) -> Result { 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 { // 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(num_1: T, num_2: T) -> T -where - T: PartialOrd + Sub, -{ - if num_1 > num_2 { - return num_1 - num_2; - } - return num_2 - num_1; -} diff --git a/src/advent_of_code/types.rs b/src/advent_of_code/types.rs index c5d44ad..55ecfed 100644 --- a/src/advent_of_code/types.rs +++ b/src/advent_of_code/types.rs @@ -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, +} diff --git a/src/advent_of_code/utils.rs b/src/advent_of_code/utils.rs index f80bfb0..8b346e8 100644 --- a/src/advent_of_code/utils.rs +++ b/src/advent_of_code/utils.rs @@ -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, Vec) { return (list_1, list_2); } + +pub fn read_report_list(input: &str) {} + +pub fn calc_distance(num_1: T, num_2: T) -> T +where + T: PartialOrd + Sub, +{ + if num_1 > num_2 { + return num_1 - num_2; + } + return num_2 - num_1; +}