Skip to content

Commit

Permalink
day 2
Browse files Browse the repository at this point in the history
  • Loading branch information
lawandothman committed Dec 2, 2024
1 parent 2497bb8 commit fe2c3d3
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
6 changes: 6 additions & 0 deletions data/examples/02.txt
Original file line number Diff line number Diff line change
@@ -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
76 changes: 76 additions & 0 deletions src/bin/02.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
advent_of_code::solution!(2);

fn is_safe(levels: &[i32]) -> bool {
let is_increasing = levels.windows(2).all(|pair| pair[1] > pair[0]);
let is_decreasing = levels.windows(2).all(|pair| pair[1] < pair[0]);

if !is_increasing && !is_decreasing {
return false;
}

levels.windows(2).all(|pair| {
let diff = (pair[1] - pair[0]).abs();
(1..=3).contains(&diff)
})
}

pub fn part_one(input: &str) -> Option<u32> {
let count = input
.lines()
.filter(|report| {
let levels: Vec<i32> = report
.split_whitespace()
.map(|n| n.parse::<i32>().expect("Invalid number"))
.collect();

is_safe(&levels)
})
.count();

Some(count as u32)
}

pub fn part_two(input: &str) -> Option<u32> {
let count = input
.lines()
.filter(|report| {
let levels: Vec<i32> = report
.split_whitespace()
.map(|n| n.parse::<i32>().expect("Invalid number"))
.collect();

if is_safe(&levels) {
return true;
}

for i in 0..levels.len() {
let mut modified = levels.clone();
modified.remove(i);

if is_safe(&modified) {
return true;
}
}
false
})
.count();

Some(count as u32)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(2));
}

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(4));
}
}

0 comments on commit fe2c3d3

Please sign in to comment.