Skip to content

Commit afd1a1a

Browse files
committed
Make day 2 cleaner
1 parent 2e4b2a4 commit afd1a1a

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

src/bin/02.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,27 @@
1+
use std::cmp::Ordering::Equal;
2+
use itertools::Itertools;
3+
14
advent_of_code::solution!(2);
25

36
fn is_safe(data: &Vec<u32>) -> bool {
4-
let mut previous_direction = None;
5-
for i in 0..(data.len() - 1) {
6-
let diff = data[i].abs_diff(data[i + 1]);
7-
if diff == 0 || diff > 3 {
8-
return false;
9-
}
10-
let direction = data[i].cmp(&data[i + 1]);
11-
match previous_direction {
12-
Some(previous_direction) => if previous_direction != direction {
13-
return false;
14-
},
15-
None => previous_direction = Some(direction)
16-
}
7+
let direction = data[0].cmp(&data[1]);
8+
if direction == Equal {
9+
return false;
1710
}
18-
true
11+
data.iter().tuple_windows::<(&u32, &u32)>()
12+
.map(|(current, next)| (1..=3).contains(&current.abs_diff(*next)) && current.cmp(next) == direction)
13+
.all(|valid| valid)
1914
}
2015

2116
fn is_safe_lenient(data: &Vec<u32>) -> bool {
2217
if is_safe(data) {
2318
return true;
2419
}
25-
for i in 0..data.len() {
20+
(0..data.len()).any(|index| {
2621
let mut modified_data = data.clone();
27-
modified_data.remove(i);
28-
if is_safe(&modified_data) {
29-
return true
30-
}
31-
}
32-
false
22+
modified_data.remove(index);
23+
is_safe(&modified_data)
24+
})
3325
}
3426

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

0 commit comments

Comments
 (0)