-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.rs
76 lines (68 loc) · 1.5 KB
/
day2.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
73
74
75
76
use aoc_runner_derive::aoc;
#[inline]
fn parse_line(line: &str) -> smallvec::SmallVec<[u8; 8]> {
line.split_ascii_whitespace()
.map(|num| num.parse().unwrap())
.collect()
}
#[inline]
fn check_values(values: &[u8]) -> bool {
let mut inc = true;
let mut dec = true;
for win in values.windows(2) {
if win[0].abs_diff(win[1]) > 3 {
return false;
}
inc &= win[0] < win[1];
dec &= win[0] > win[1];
if !(inc ^ dec) {
return false;
}
}
true
}
#[aoc(day2, part1)]
#[must_use]
pub fn part1(input: &str) -> usize {
input
.lines()
.filter(|line| check_values(&parse_line(line)))
.count()
}
#[aoc(day2, part2)]
#[must_use]
pub fn part2(input: &str) -> usize {
input
.lines()
.filter(|line| {
let values = parse_line(line);
check_values(&values)
|| (0..values.len()).any(|i| {
let mut values = values.clone();
values.remove(i);
check_values(&values)
})
})
.count()
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
const SAMPLE: &str = indoc! {"
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]
pub fn part1_example() {
assert_eq!(part1(SAMPLE), 2);
}
#[test]
pub fn part2_example() {
assert_eq!(part2(SAMPLE), 4);
}
}