day 2: input read from file

This commit is contained in:
2024-12-03 18:02:02 +01:00
parent f30a2ff86f
commit 85f7ac2753
4 changed files with 1026 additions and 13 deletions

View File

@@ -17,6 +17,6 @@ pub fn historian_hysteria() {
println!("The similarity is: {similarity}");
// Monday 02
let safe_report_count = mon_02::check_reports_safety("");
let safe_report_count = mon_02::check_reports_safety("./assets/day_2_reports_input");
println!("There are {safe_report_count} safe reports");
}

View File

@@ -1,16 +1,9 @@
use utils::calc_distance;
use utils::{calc_distance, read_report_list};
use super::*;
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 report_list = read_report_list(input);
let mut safe_count: ReportSafety = 0;
@@ -25,6 +18,7 @@ pub fn check_reports_safety(input: &str) -> ReportSafety {
let mut safe = true;
let initial_direction: ReportDirection = get_report_direction(&report[0..=1]);
let mut problems_damped_count = 0;
'report_check: for index in 1..report.len() {
let prev = index - 1;
@@ -36,8 +30,11 @@ pub fn check_reports_safety(input: &str) -> ReportSafety {
|| distance > 3
|| direction != initial_direction
{
safe = false;
break 'report_check;
if report_problem_dampener(&report) == false {
safe = false;
break 'report_check;
}
problems_damped_count += 1;
}
}
@@ -49,6 +46,9 @@ pub fn check_reports_safety(input: &str) -> ReportSafety {
safe_count
}
// NOTE: the simples solition is by brute force...
fn report_problem_dampener(report: &Vec<Report>) -> bool {}
// 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 {

View File

@@ -17,7 +17,20 @@ 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 read_report_list(input: &str) -> Vec<Vec<Report>> {
let mut report_list: Vec<Vec<Report>> = vec![];
for report in read_to_string(input).unwrap().lines() {
let level_list = report.split(" ");
let mut report_vec: Vec<Report> = vec![];
for level in level_list {
report_vec.push(level.parse::<Report>().unwrap());
}
report_list.push(report_vec);
}
report_list
}
pub fn calc_distance<T>(num_1: T, num_2: T) -> T
where