From 979f4e348a8bf9b62cc2fb4aec599ea606ac8919 Mon Sep 17 00:00:00 2001 From: Dan Chadwick Date: Sun, 8 Dec 2024 21:52:32 -0500 Subject: [PATCH] Updating so each solution is printed. --- 2024/rust/src/day2.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/2024/rust/src/day2.rs b/2024/rust/src/day2.rs index 56e1b16..153edf2 100644 --- a/2024/rust/src/day2.rs +++ b/2024/rust/src/day2.rs @@ -6,26 +6,24 @@ pub fn run_day_2() -> Solution { // Fetch the file. let reports_file = fs::read_to_string("./inputs/day2.txt").expect("unable to read file"); // Init the safe reports count. - let mut safe_reports = 0; + let mut safe_reports_p1 = 0; + let mut safe_reports_p2 = 0; // iterate over each line to check if it matches the conditions. for line in reports_file.lines() { - let failures_on_line: u32 = 0; - let mut increasing_or_decreasing = false; - // cast to string. let line_string = line.to_string(); // create a vector with each character for iteration. let report_numbers: Vec = line_string.split_whitespace().map(|s| s.parse().expect("Conversion Failed")).collect(); // Now we have report_nums populated with the numbers to check. let valid_report = validate(&report_numbers); if valid_report { - safe_reports += 1; + safe_reports_p1 += 1; + safe_reports_p2 += 1; continue; } - // iterate x amount of times and check if valid. + // remove each item one at a time and re-validate. let mut dampened = false; for i in 0..report_numbers.len() { let mut report_copy = report_numbers.clone(); - // handle the first iteration. report_copy.remove(i); if validate(&report_copy) { dampened = true; @@ -34,15 +32,15 @@ pub fn run_day_2() -> Solution { } if dampened { - safe_reports += 1; + safe_reports_p2 += 1; } } // Final solutions go here. let solution = Solution { day: 2, - answer_1: "463".to_string(), - answer_2: safe_reports.to_string(), + answer_1: safe_reports_p1.to_string(), + answer_2: safe_reports_p2.to_string(), }; solution