-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathd4.rs
48 lines (43 loc) · 890 Bytes
/
d4.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
use std::collections::HashMap;
fn counter(iter: &str) -> HashMap<char, usize> {
let mut d = HashMap::new();
for elem in iter.chars() {
let count = d.entry(elem).or_insert(0);
*count += 1;
}
d
}
fn valid<F>(code: usize, pred: F) -> bool
where
F: Fn(&Vec<&usize>) -> bool,
{
let scode = code.to_string();
if !scode[..]
.chars()
.zip(scode[1..].chars())
.all(|(a, b)| b >= a)
{
return false;
}
let c = counter(&scode);
let counts: Vec<&usize> = c.values().collect();
pred(&counts)
}
fn main() {
let a = 284639;
let b = 748759;
println!(
"{}",
(a..b)
.map(|n| valid(n, |counts| counts.iter().any(|&c| *c > 1)))
.map(|b| b as usize)
.sum::<usize>()
);
println!(
"{}",
(a..b)
.map(|n| valid(n, |counts| counts.iter().any(|&c| *c == 2)))
.map(|b| b as usize)
.sum::<usize>()
);
}