Updating so each solution is printed.

This commit is contained in:
Dan Chadwick 2024-12-08 21:52:32 -05:00
parent 1a692b2aec
commit 979f4e348a

View File

@ -6,26 +6,24 @@ pub fn run_day_2() -> Solution {
// Fetch the file. // Fetch the file.
let reports_file = fs::read_to_string("./inputs/day2.txt").expect("unable to read file"); let reports_file = fs::read_to_string("./inputs/day2.txt").expect("unable to read file");
// Init the safe reports count. // 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. // iterate over each line to check if it matches the conditions.
for line in reports_file.lines() { 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(); let line_string = line.to_string();
// create a vector with each character for iteration. // create a vector with each character for iteration.
let report_numbers: Vec<u32> = line_string.split_whitespace().map(|s| s.parse().expect("Conversion Failed")).collect(); let report_numbers: Vec<u32> = line_string.split_whitespace().map(|s| s.parse().expect("Conversion Failed")).collect();
// Now we have report_nums populated with the numbers to check. // Now we have report_nums populated with the numbers to check.
let valid_report = validate(&report_numbers); let valid_report = validate(&report_numbers);
if valid_report { if valid_report {
safe_reports += 1; safe_reports_p1 += 1;
safe_reports_p2 += 1;
continue; continue;
} }
// iterate x amount of times and check if valid. // remove each item one at a time and re-validate.
let mut dampened = false; let mut dampened = false;
for i in 0..report_numbers.len() { for i in 0..report_numbers.len() {
let mut report_copy = report_numbers.clone(); let mut report_copy = report_numbers.clone();
// handle the first iteration.
report_copy.remove(i); report_copy.remove(i);
if validate(&report_copy) { if validate(&report_copy) {
dampened = true; dampened = true;
@ -34,15 +32,15 @@ pub fn run_day_2() -> Solution {
} }
if dampened { if dampened {
safe_reports += 1; safe_reports_p2 += 1;
} }
} }
// Final solutions go here. // Final solutions go here.
let solution = Solution { let solution = Solution {
day: 2, day: 2,
answer_1: "463".to_string(), answer_1: safe_reports_p1.to_string(),
answer_2: safe_reports.to_string(), answer_2: safe_reports_p2.to_string(),
}; };
solution solution