diff options
author | Guillermo Ramos | 2024-12-04 10:28:51 +0100 |
---|---|---|
committer | Guillermo Ramos | 2024-12-04 10:51:41 +0100 |
commit | 1c9620d4666fdf41ef751f100dfb17f14133dd35 (patch) | |
tree | ece2ef499492b2426b13bd9b48f98f35a60123c2 /2024/2/p2.rs | |
parent | a42dc9c3efb52a74fc581835f585679e8a5e2bb1 (diff) | |
download | AoC-1c9620d4666fdf41ef751f100dfb17f14133dd35.tar.gz |
2024: move to single Rust crate
Diffstat (limited to '2024/2/p2.rs')
-rw-r--r-- | 2024/2/p2.rs | 53 |
1 files changed, 0 insertions, 53 deletions
diff --git a/2024/2/p2.rs b/2024/2/p2.rs deleted file mode 100644 index 78224c4..0000000 --- a/2024/2/p2.rs +++ /dev/null @@ -1,53 +0,0 @@ -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 { - unreachable!() - }; - let (diff, d) = if x > y { (x - y, Down) } else { (y - x, Up) }; - if let Unknown = direction { - direction = d; - } - if diff == 0 || diff > 3 || direction != d { - return false; - } - } - return true; -} - -fn is_safe_with_dampener(levels: &[u32]) -> bool { - for i in 0..levels.len() { - let mut levels_without_i: Vec<u32> = levels.to_vec(); - levels_without_i.remove(i); - if is_safe(&levels_without_i) { - return true; - } - } - return false; -} - -fn main() { - let input = std::fs::read_to_string(INPUT_FILE).unwrap(); - - let mut total = 0; - for report in input.lines() { - let levels: Vec<u32> = report - .split_whitespace() - .map(|l| l.parse().unwrap()) - .collect(); - if is_safe(&levels) || is_safe_with_dampener(&levels) { - total += 1; - } - } - println!("Levels! {:?}", total); -} |