-
Notifications
You must be signed in to change notification settings - Fork 1
/
4b.rs
57 lines (49 loc) · 1.19 KB
/
4b.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
49
50
51
52
53
54
55
56
57
use std::error::Error;
fn check_two_adjacent_eq(pwd: &[u8; 6]) -> bool {
let mut current_digit = None;
let mut run_size = 0;
for i in 0..pwd.len() {
if Some(pwd[i]) == current_digit {
run_size += 1;
continue;
}
if run_size == 2 {
return true;
}
current_digit = Some(pwd[i]);
run_size = 1;
}
return run_size == 2;
}
fn check_non_decreasing(pwd: &[u8; 6]) -> bool {
(0..pwd.len()-1).all(|i| pwd[i] <= pwd[i + 1])
}
fn check(pwd: &[u8; 6]) -> bool {
check_two_adjacent_eq(pwd) && check_non_decreasing(pwd)
}
fn bump(pwd: &mut [u8; 6]) {
for digit in pwd.iter_mut().rev() {
if *digit < 9 {
*digit += 1;
return;
}
*digit = 0;
}
}
fn main() -> Result<(), Box<dyn Error>> {
assert!(check(&[1,1,2,2,3,3]));
assert!(!check(&[1,2,3,4,4,4]));
assert!(check(&[1,1,1,1,2,2]));
let first = [2,4,0,9,2,0];
let last = [7,8,9,8,5,7];
let mut cnt = 0;
let mut pwd = first.clone();
while pwd != last {
if check(&pwd) {
cnt += 1;
}
bump(&mut pwd);
}
println!("{}", cnt);
Ok(())
}