blob: c40e3375537d46c62b19fc36d11617790c42fe8a (
plain)
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
|
use std::fs;
fn is_nice_round1(line: &str) -> bool {
let do_not_want: [&str; 4] = ["ab", "cd", "pq", "xy"];
let mut unwanted_substr = false;
let mut has_consecutive = false;
let mut vowels = 0;
for i in 0..line.len() - 1 {
let substr: &str = &line[i..i + 2];
if do_not_want.iter().position(|&x| x == substr).is_some() {
unwanted_substr = true;
break;
}
if "aeiou".contains(substr.chars().nth(0).unwrap()) {
vowels += 1;
}
if substr.chars().nth(0).unwrap() == substr.chars().nth(1).unwrap() {
has_consecutive = true;
}
}
if "aeiou".contains(line.chars().nth(line.len() - 1).unwrap()) {
vowels += 1;
}
if unwanted_substr {
return false;
}
return vowels >= 3 && has_consecutive;
}
fn main() {
let binding = fs::read_to_string("5-input").expect("wget 5-input please.");
let contents: Vec<&str> = binding.split("\n").collect();
let nice_lines_1 = contents.iter().filter(|&x| is_nice_round1(x)).count();
println!("Round 1: {nice_lines_1}");
}
// Local Variables:
// compile-command: "rustc puzzle-5.rs && ./puzzle-5"
// End:
|