-
Notifications
You must be signed in to change notification settings - Fork 7
/
02.rs
38 lines (33 loc) · 1.19 KB
/
02.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
use itertools::Itertools;
use std::io::stdin;
struct PasswordPhilosophy {
range: std::ops::RangeInclusive<usize>,
letter: char,
string: String,
}
fn parse_password_philosophies() -> Vec<PasswordPhilosophy> {
let lines = stdin().lines().filter_map(Result::ok).collect_vec();
lines.iter()
.map(|line| line.split(|c| "- :".contains(c)).next_tuple().unwrap())
.map(|(a, b, letter, _, string)| {
PasswordPhilosophy {
range: a.parse().unwrap()..=b.parse().unwrap(),
letter: letter.chars().next().unwrap(),
string: string.to_owned(),
}
})
.collect()
}
fn main() {
let philosophies = parse_password_philosophies();
println!("{}", philosophies.iter().filter(|p| p.range.contains(&p.string.matches(p.letter).count())).count());
let sum2 = philosophies
.iter()
.filter(|p| {
let is_first = p.string.chars().nth((p.range.start() - 1) as usize).unwrap() == p.letter;
let is_second = p.string.chars().nth((p.range.end() - 1) as usize).unwrap() == p.letter;
is_first != is_second
})
.count();
println!("{}", sum2);
}