-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
87 lines (81 loc) · 2 KB
/
main.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
fn is_possible_password(n: usize) -> bool {
let mut last = None;
let mut double = false;
for digit in n
.to_string()
.chars()
.map(|d| d.to_digit(10).unwrap())
.collect::<Vec<_>>()
.into_iter()
{
if Some(digit) == last {
double = true;
}
if let Some(l) = last {
if digit < l {
return false;
}
}
last = Some(digit);
}
return double;
}
fn part1(low: usize, high: usize) -> usize {
let mut count: usize = 0;
for n in low..=high {
if is_possible_password(n) {
count += 1
}
}
count
}
fn is_possible_password2(n: usize) -> bool {
let digits = n
.to_string()
.chars()
.map(|d| d.to_digit(10).unwrap())
.collect::<Vec<_>>();
let mut prev: u32 = digits[0];
let mut group_size = 1;
let mut found_double = false;
for digit in digits.into_iter().skip(1) {
if digit == prev {
group_size += 1;
} else {
if digit < prev {
return false;
}
if group_size == 2 {
found_double = true;
}
prev = digit;
group_size = 1;
}
}
found_double || group_size == 2
}
fn part2(low: usize, high: usize) -> usize {
let mut count: usize = 0;
for n in low..=high {
if is_possible_password2(n) {
count += 1
}
}
count
}
fn main() {
println!("Part 1: {}", part1(138241, 674034));
println!("Part 2: {}", part2(138241, 674034));
}
#[test]
fn test_is_possible_password() {
assert_eq!(is_possible_password(111111), true);
assert_eq!(is_possible_password(223450), false);
assert_eq!(is_possible_password(123789), false);
}
#[test]
fn test_is_possible_password2() {
assert_eq!(is_possible_password2(112233), true);
assert_eq!(is_possible_password2(123444), false);
assert_eq!(is_possible_password2(111122), true);
}