-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
51 lines (43 loc) · 1.05 KB
/
main.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
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one(data));
println!("Part 2: {}", part_two(data));
}
fn part_one(data: &str) -> usize {
data.lines().map(|l| find_window(l, 4)).sum()
}
fn part_two(data: &str) -> usize {
data.lines().map(|l| find_window(l, 14)).sum()
}
fn find_window(line: &str, len: usize) -> usize {
for (i, window) in line.as_bytes().windows(len).enumerate() {
if unique(window) {
return i + len;
}
}
0
}
fn unique(letters: &[u8]) -> bool {
if let Some((first, rest)) = letters.split_first() {
if rest.contains(first) {
return false;
} else {
return unique(rest);
}
}
true
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
assert_eq!(7 + 5 + 6 + 10 + 11, part_one(data));
}
#[test]
fn two() {
let data = include_str!("test.txt");
assert_eq!(19 + 23 + 23 + 29 + 26, part_two(data));
}
}