-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
81 lines (74 loc) · 2.28 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
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
81
fn is_nice(s: &str) -> bool {
let vowels = s
.chars()
.filter(|c| match c {
'a' | 'e' | 'i' | 'o' | 'u' => true,
_ => false,
})
.count();
let has_double_letter = s
.chars()
.collect::<Vec<char>>()
.windows(2)
.any(|c| c[0] == c[1]);
let has_naughty_seq = s.chars().collect::<Vec<char>>().windows(2).any(|c| {
match c.into_iter().collect::<String>().as_str() {
"ab" | "cd" | "pq" | "xy" => true,
_ => false,
}
});
vowels >= 3 && has_double_letter && !has_naughty_seq
}
fn is_nice2(s: &str) -> bool {
let x = String::from(s);
let has_double_pair =
s.chars()
.collect::<Vec<char>>()
.windows(2)
.enumerate()
.any(|(idx, chars)| {
let pair = chars.into_iter().collect::<String>();
if let Some(f) = x.rfind(&pair) {
f > idx + 1
} else {
false
}
});
let has_pair_with_blank = s
.chars()
.collect::<Vec<char>>()
.windows(3)
.any(|c| c[0] == c[2] && c[0] != c[1]);
has_double_pair && has_pair_with_blank
}
fn main() {
let nice_count = include_str!("../in.txt")
.split("\n")
.map(|line| is_nice(line))
.fold(0, |count, naughty| count + if naughty { 1 } else { 0 });
println!("Part 1: {}", nice_count);
let nice2_count = include_str!("../in.txt")
.split("\n")
.map(|line| is_nice2(line))
.fold(0, |count, naughty| count + if naughty { 1 } else { 0 });
println!("Part 2: {}", nice2_count);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_naughty() {
assert_eq!(is_nice("ugknbfddgicrmopn"), true);
assert_eq!(is_nice("aaa"), true);
assert_eq!(is_nice("jchzalrnumimnmhp"), false);
assert_eq!(is_nice("haegwjzuvuyypxyu"), false);
assert_eq!(is_nice("dvszwmarrgswjxmb"), false);
}
#[test]
fn test_is_naughty2() {
assert_eq!(is_nice2("qjhvhtzxzqqjkmpb"), true);
assert_eq!(is_nice2("xxyxx"), true);
assert_eq!(is_nice2("uurcxstgmygtbstg"), false);
assert_eq!(is_nice2("ieodomkazucvgmuy"), false);
}
}