-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.rs
72 lines (63 loc) · 1.62 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
fn inspect_report(report: &[u8]) -> bool {
if report[0] < report[report.len() - 1] {
report.windows(2).all(|v| v[0] < v[1] && v[1] - v[0] <= 3)
} else {
report.windows(2).all(|v| v[0] > v[1] && v[0] - v[1] <= 3)
}
}
#[derive(Clone)]
struct Solution {
reports: Vec<Vec<u8>>,
}
impl Solver for Solution {
fn new(input: &str) -> Anyhow<Self> {
Ok(Self {
reports: input
.lines()
.map(|line| NumberParser::from(line).collect())
.collect(),
})
}
fn part1(&mut self) -> Anyhow<impl fmt::Display> {
Ok(self
.reports
.iter()
.filter(|report| inspect_report(report))
.count())
}
fn part2(&mut self) -> Anyhow<impl fmt::Display> {
Ok(self
.reports
.par_iter()
.filter(|report| {
(0..report.len()).any(|i| {
inspect_report(&[&report[0..i], &report[i + 1..report.len()]].concat())
})
})
.count())
}
}
aoc::solution!();
#[cfg(test)]
mod test {
use super::{Solution, Solver};
const INPUT: &str = r"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
";
#[test]
fn test_part1() {
let mut solution = Solution::new(INPUT).unwrap();
let answer = solution.part1().unwrap().to_string();
assert_eq!(answer, "2");
}
#[test]
fn test_part2() {
let mut solution = Solution::new(INPUT).unwrap();
let answer = solution.part2().unwrap().to_string();
assert_eq!(answer, "4");
}
}