-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.rs
80 lines (72 loc) · 1.96 KB
/
day06.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
77
78
79
80
use std::collections::HashSet;
pub fn solution(input: &str, win_size: usize) -> usize {
input
.as_bytes()
.windows(win_size)
.enumerate()
.find_map(|(idx, w)| {
if w.iter().collect::<HashSet<_>>().len() != w.len() {
None
} else {
Some(idx + win_size)
}
})
.unwrap()
}
pub fn part_one(input: &str) -> usize {
solution(input, 4)
}
pub fn part_two(input: &str) -> usize {
solution(input, 14)
}
#[cfg(test)]
mod tests {
#[test]
fn part_one_example() {
assert_eq!(
super::part_one(include_str!("input/day06_example_one.txt")), 7
);
assert_eq!(
super::part_one(include_str!("input/day06_example_two.txt")), 5
);
assert_eq!(
super::part_one(include_str!("input/day06_example_three.txt")), 6
);
assert_eq!(
super::part_one(include_str!("input/day06_example_four.txt")), 10
);
assert_eq!(
super::part_one(include_str!("input/day06_example_five.txt")), 11
);
}
#[test]
fn part_one() {
assert_eq!(
super::part_one(include_str!("input/day06.txt")), 1920
);
}
#[test]
fn part_two_example() {
assert_eq!(
super::part_two(include_str!("input/day06_example_one.txt")), 19
);
assert_eq!(
super::part_two(include_str!("input/day06_example_two.txt")), 23
);
assert_eq!(
super::part_two(include_str!("input/day06_example_three.txt")), 23
);
assert_eq!(
super::part_two(include_str!("input/day06_example_four.txt")), 29
);
assert_eq!(
super::part_two(include_str!("input/day06_example_five.txt")), 26
);
}
#[test]
fn part_two() {
assert_eq!(
super::part_two(include_str!("input/day06.txt")), 2334
);
}
}