From d69b3025ffe15cc38844704c79a8fa23fe96f09b Mon Sep 17 00:00:00 2001 From: Guillermo Ramos Date: Mon, 2 Dec 2024 10:58:34 +0100 Subject: 2024.2 --- 2024/2/p1.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2024/2/p1.rs (limited to '2024/2/p1.rs') diff --git a/2024/2/p1.rs b/2024/2/p1.rs new file mode 100644 index 0000000..4e4df46 --- /dev/null +++ b/2024/2/p1.rs @@ -0,0 +1,38 @@ +const INPUT_FILE: &str = "input"; + +#[derive(PartialEq, Clone, Copy)] +enum Direction { + Up, + Down, + Unknown, +} +use Direction::*; + +fn is_safe(levels: &[u32]) -> bool { + let mut direction = Unknown; + for i in 0..levels.len()-1 { + let [x, y] = levels[i..=i+1] else { panic!("unreachable") }; + let (diff, d) = if x > y { (x-y, Down) } else { (y-x, Up) }; + if direction == Unknown { + direction = d; + } + if diff == 0 || diff > 3 || direction != d { + return false; + } + } + return true; +} + +fn main() { + let input = std::fs::read_to_string(INPUT_FILE).unwrap(); + + let mut total = 0; + for report in input.lines() { + let levels: Vec = + report.split_whitespace().map(|l| l.parse().unwrap()).collect(); + if is_safe(&levels) { + total += 1; + } + } + println!("Levels! {:?}", total); +} -- cgit v1.2.3