-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.rs
70 lines (58 loc) · 1.41 KB
/
5.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
const INPUT: &str = include_str!("5.txt");
fn main() { one(); two(); }
fn one() {
let result = INPUT
.lines()
.filter(|line| {
!has_forbidden_sequences(line)
&& has_double_letters(line)
&& has_three_vowels(line)
})
.count();
print!("5.1: {result}\t\t");
}
fn two() {
let result = INPUT
.lines()
.filter(|line| {
has_two_letter_pairs(line)
&& has_repeating_letter(line)
})
.count();
println!("5.2: {result}");
}
fn has_three_vowels(line: &str) -> bool {
line
.chars()
.filter(|c| ['a', 'e', 'i', 'o', 'u'].contains(&c))
.count()
>= 3
}
fn has_double_letters(line: &str) -> bool {
for i in 1..line.len() {
if line.chars().nth(i - 1).unwrap() == line.chars().nth(i).unwrap() {
return true;
}
}
false
}
fn has_forbidden_sequences(line: &str) -> bool {
for seq in ["ab", "cd", "pq", "xy"] {
if line.contains(seq) { return true; }
}
false
}
fn has_two_letter_pairs(line: &str) -> bool {
for i in 0..(line.len() - 3) {
if line[i + 2..].contains(&line[i..=i + 1]) { return true; }
}
false
}
fn has_repeating_letter(line: &str) -> bool {
for i in 0..(line.len() - 2) {
if line.chars().nth(i) == line.chars().nth(i + 2) {
return true;
}
}
false
}