From 6a3a324ea1e49b2c576a8e9d167ec358378ff314 Mon Sep 17 00:00:00 2001 From: James Musselman Date: Mon, 16 Dec 2024 18:00:57 -0600 Subject: [PATCH] Add part one of day 2 --- data/examples/02.txt | 6 ++++++ src/bin/02.rs | 20 ++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/data/examples/02.txt b/data/examples/02.txt index e69de29..b49c10d 100644 --- a/data/examples/02.txt +++ b/data/examples/02.txt @@ -0,0 +1,6 @@ +7 6 4 2 1 +1 2 7 8 9 +9 7 6 2 1 +1 3 2 4 5 +8 6 4 4 1 +1 3 6 7 9 diff --git a/src/bin/02.rs b/src/bin/02.rs index 3bae35c..a6633ba 100644 --- a/src/bin/02.rs +++ b/src/bin/02.rs @@ -1,7 +1,23 @@ advent_of_code::solution!(2); pub fn part_one(input: &str) -> Option { - None + let mut num_safe: u32 = 0; + let lines = input.lines(); + for line in lines { + let levels: Vec = line + .split_whitespace() + .filter_map(|i| i.parse::().ok()) + .collect(); + + if levels + .windows(2) + .all(|w| (w[0] > w[1] && w[0] - w[1] <= 3) || (w[1] > w[0] && w[1] - w[0] <= 3)) + && (levels.windows(2).all(|w| w[0] < w[1]) || levels.windows(2).all(|w| w[0] > w[1])) + { + num_safe += 1; + } + } + Some(num_safe) } pub fn part_two(input: &str) -> Option { @@ -15,7 +31,7 @@ mod tests { #[test] fn test_part_one() { let result = part_one(&advent_of_code::template::read_file("examples", DAY)); - assert_eq!(result, None); + assert_eq!(result, Some(2)); } #[test]