-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path04.rs
More file actions
39 lines (35 loc) · 946 Bytes
/
04.rs
File metadata and controls
39 lines (35 loc) · 946 Bytes
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
use std::time::Instant;
use std::ops::Range;
use itertools::Itertools;
const INPUT: Range<usize> = 206938..679128;
fn to_digits(n: usize) -> [usize; 6] {
[
(n / 100000) % 10,
(n / 10000) % 10,
(n / 1000) % 10,
(n / 100) % 10,
(n / 10) % 10,
(n / 1) % 10,
]
}
fn count_passwords() -> (usize,usize) {
let passwords = INPUT.map(to_digits)
.filter(|d| (1..d.len()).all(|i| d[i-1] <= d[i]))
.filter(|d| (1..d.len()).any(|i| d[i-1] == d[i]))
.map(|d| d.iter()
.group_by(|&i| i)
.into_iter()
.any(|(_,x)| x.count() == 2)
)
.collect::<Vec<_>>();
let part_one = passwords.len();
let part_two = passwords.iter().filter(|&&b| b).count();
(part_one,part_two)
}
fn main() {
let now = Instant::now();
let (part_one, part_two) = count_passwords();
println!("Part one: {}", part_one);
println!("Part two: {}", part_two);
println!("Time: {}ms", now.elapsed().as_millis());
}