-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.rs
55 lines (46 loc) · 1.12 KB
/
day02.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use std::collections::HashSet;
use aoc_core::prelude::*;
fn part1(input: &Input) -> isize {
input.lines()
.map(|x| x.integers().collect::<Vec<_>>())
.filter(|x| is_safe(x))
.count()
.try_into()
.unwrap()
}
fn part2(input: &Input) -> isize {
input.lines()
.map(|x| x.integers().collect::<Vec<_>>())
.filter(|x| {
(0..x.len()).any(|i| {
let mut new_values = x.clone();
new_values.remove(i);
is_safe(&new_values)
})
})
.count()
.try_into()
.unwrap()
}
fn is_safe(levels: &[isize]) -> bool {
let diff = levels
.windows(2)
.map(|x| x[0] - x[1]);
diff.clone().map(isize::signum).collect::<HashSet<isize>>().len() == 1 &&
diff.clone().all(|x| x.abs() <= 3)
}
runner!();
#[cfg(test)]
#[test]
fn test() {
let input = Input::new("
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
".to_string());
assert_eq!(part1(&input), 2, "Part 1");
assert_eq!(part2(&input), 4, "Part 2");
}