blob: 461e60bb7846e65f32231ebe5bbbe1a8b2cc9702 (
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
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::fs;
const VOWELS_STR: &str = "aeiou";
const DO_NOT_WANT: [&str; 4] = ["ab", "cd", "pq", "xy"];
fn is_nice_round1(line: &str) -> bool {
let mut unwanted_substr = false;
let mut has_consecutive = false;
let mut vowels = 0;
for i in 0..line.len() - 1 {
let slide: &str = &line[i..i + 2];
if DO_NOT_WANT.iter().position(|&x| x == slide).is_some() {
unwanted_substr = true;
break;
}
let first = slide.chars().nth(0).unwrap();
let second = slide.chars().nth(1).unwrap();
if VOWELS_STR.contains(first) {
vowels += 1;
}
if first == second {
has_consecutive = true;
}
}
if VOWELS_STR.contains(line.chars().nth(line.len() - 1).unwrap()) {
vowels += 1;
}
if unwanted_substr {
return false;
}
return vowels >= 3 && has_consecutive;
}
fn is_nice_round2(line: &str) -> bool {
/*
Justifying the O(|line|^2) runtime is easy: |line| is constant, such that at
worst we're doing roughly O(256) iterations. Not too bad.
If |line| could be really big I'd look into optimising the rest, but why do
that?
*/
let mut has_pair = false;
// Unfortunate O(|line|^2) runtime
for i in 0..line.len() - 1 {
let slide = &line[i..i + 2];
let rest = &line[i + 2..];
if rest.contains(slide) {
has_pair = true;
break;
}
}
// O(|line|) runtime
let mut has_triple = false;
for i in 0..line.len() - 2 {
let slide = &line[i..i + 3];
if slide.chars().nth(0) == slide.chars().nth(2) {
has_triple = true;
break;
}
}
return has_pair && has_triple;
}
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();
let nice_lines_2 = contents.iter().filter(|&x| is_nice_round2(x)).count();
println!("Round 1: {nice_lines_1}");
println!("Round 2: {nice_lines_2}");
}
// Local Variables:
// compile-command: "rustc puzzle-5.rs && ./puzzle-5"
// End:
|