more progress

This commit is contained in:
2025-09-02 09:42:27 -04:00
parent 46b91dae30
commit 1776ed8f57
6 changed files with 341 additions and 60 deletions
+25 -32
View File
@@ -38,12 +38,13 @@
// So, in this example, 2 reports are safe.
use std::{env, fs};
use core::read_data;
fn min_max(num1: u32, num2: u32) -> (u32, u32) {
(num1.min(num2), num1.max(num2))
}
fn in_range(min: u32, max:u32, target: u32) -> bool {
fn in_range(min: u32, max: u32, target: u32) -> bool {
min <= target && target <= max
}
@@ -54,50 +55,42 @@ fn parse_numbers(s: &str) -> Vec<u32> {
}
fn is_safe(report: &str) -> bool {
let mut is_safe = true;
let mut last_reading = 0;
let numbers = parse_numbers(report);
println!("numbers: {:?}", numbers);
println!("*******************************************************\nnumbers: {:?}", numbers);
let mut is_increasing = numbers[0] < numbers[1];
println!("Checking report {report}");
println!("Found is_increasing = {is_increasing} from {}/{}", numbers[0], numbers[1]);
for index in 0..numbers.len() - 1 {
let first = numbers[index];
let second = numbers[index + 1];
for (index, reading) in numbers.iter().enumerate() {
println!("Checking {index}->{reading} in {report} / {is_increasing}");
if first.abs_diff(second) > 3 || first.abs_diff(second) < 1 {
is_safe = false;
}
if is_increasing && first >= second {
is_safe = false;
}
if !is_increasing && first <= second {
is_safe = false;
}
}
// for (index, reading) in numbers.enumerate() {
// let reading_u32: u32 = reading.parse().unwrap();
// if index == 0 {
// // prime it for the report
// last_reading = reading_u32;
// } else if index == 1 {
// if last_reading > reading_u32 {
// is_increasing = false;
// }
// } else {
// let (min,max) = min_max(last_reading as u32, reading_u32);
//
// // changing too fast
// if (max - min) > 2 { is_safe = false; }
// }
// }
is_safe
}
fn main() {
let input_file = format!("{}/{}" ,
std::env::var("CARGO_MANIFEST_DIR").unwrap(),
"data/02_data.txt"
);
let binding = fs::read_to_string(input_file).unwrap();
let reports = binding.lines();
let input_file = read_data("2024_02_data.txt");
let mut num_safe = 0;
// let input_file = "7 6 4 2 1\n1 2 7 8 9\n9 7 6 2 1\n1 3 2 4 5\n8 6 4 4 1\n1 3 6 7 9";
let reports = input_file.lines();
for report in reports {
if is_safe(report) {
println!("Handling report of {report} -> Safe: {}", is_safe(report));
let is_safe = is_safe(report);
println!("Handling report of {report} -> Safe: {}",is_safe);
if is_safe {
num_safe += 1;
}
}
println!("Found {num_safe} safe readings.");
}
// 606