From ff064b6f13019b15346ff320a486d70b95d80b2a Mon Sep 17 00:00:00 2001 From: Guillermo Ramos Date: Fri, 6 Dec 2024 15:59:32 +0100 Subject: Each day is now a binary --- 2024_rust/src/day2.rs | 65 --------------------------------------------------- 1 file changed, 65 deletions(-) delete mode 100644 2024_rust/src/day2.rs (limited to '2024_rust/src/day2.rs') diff --git a/2024_rust/src/day2.rs b/2024_rust/src/day2.rs deleted file mode 100644 index f809141..0000000 --- a/2024_rust/src/day2.rs +++ /dev/null @@ -1,65 +0,0 @@ -#[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 { - 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; -} - -pub fn p1(input: &str) -> String { - 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; - } - } - - total.to_string() -} - -fn is_safe_with_dampener(levels: &[u32]) -> bool { - for i in 0..levels.len() { - let mut levels_without_i: Vec = levels.to_vec(); - levels_without_i.remove(i); - if is_safe(&levels_without_i) { - return true; - } - } - return false; -} - -pub fn p2(input: &str) -> String { - 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) || is_safe_with_dampener(&levels) { - total += 1; - } - } - - total.to_string() -} -- cgit v1.2.3