Skip to content

Commit

Permalink
Make day 2 cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
UnderscoreTud committed Dec 2, 2024
1 parent 2e4b2a4 commit afd1a1a
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions src/bin/02.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
use std::cmp::Ordering::Equal;
use itertools::Itertools;

advent_of_code::solution!(2);

fn is_safe(data: &Vec<u32>) -> bool {
let mut previous_direction = None;
for i in 0..(data.len() - 1) {
let diff = data[i].abs_diff(data[i + 1]);
if diff == 0 || diff > 3 {
return false;
}
let direction = data[i].cmp(&data[i + 1]);
match previous_direction {
Some(previous_direction) => if previous_direction != direction {
return false;
},
None => previous_direction = Some(direction)
}
let direction = data[0].cmp(&data[1]);
if direction == Equal {
return false;
}
true
data.iter().tuple_windows::<(&u32, &u32)>()
.map(|(current, next)| (1..=3).contains(&current.abs_diff(*next)) && current.cmp(next) == direction)
.all(|valid| valid)
}

fn is_safe_lenient(data: &Vec<u32>) -> bool {
if is_safe(data) {
return true;
}
for i in 0..data.len() {
(0..data.len()).any(|index| {
let mut modified_data = data.clone();
modified_data.remove(i);
if is_safe(&modified_data) {
return true
}
}
false
modified_data.remove(index);
is_safe(&modified_data)
})
}

fn count_safe_reports<P>(input: &str, predicate: P) -> u32
Expand Down

0 comments on commit afd1a1a

Please sign in to comment.