Add part one of day 2
Some checks failed
Continuous Integration / Continuous Integration (push) Has been cancelled

This commit is contained in:
James Musselman 2024-12-16 18:00:57 -06:00
parent ae09ba2cc7
commit 6a3a324ea1
Signed by: Musselman
GPG key ID: 1DAEFF35ECB5D6DB
2 changed files with 24 additions and 2 deletions

View file

@ -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

View file

@ -1,7 +1,23 @@
advent_of_code::solution!(2);
pub fn part_one(input: &str) -> Option<u32> {
None
let mut num_safe: u32 = 0;
let lines = input.lines();
for line in lines {
let levels: Vec<u32> = line
.split_whitespace()
.filter_map(|i| i.parse::<u32>().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<u32> {
@ -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]