-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathday04.rs
More file actions
56 lines (44 loc) · 1.86 KB
/
Copy pathday04.rs
File metadata and controls
56 lines (44 loc) · 1.86 KB
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
//! # Secure Container
//!
//! We speed things up by only checking numbers that have digits in non-decreasing order for pairs.
//! These numbers become rapidly less dense as the password value increases and there
//! are only 3003 total of these numbers with 6 digits.
use crate::util::iter::*;
use crate::util::parse::*;
type Input = (u32, u32);
pub fn parse(input: &str) -> Input {
let [start, end] = input.iter_unsigned::<u32>().chunk::<2>().next().unwrap();
let mut digits = to_digits(start);
let end = to_digits(end);
let mut part_one = 0;
let mut part_two = 0;
// Increase the starting number to the next number that has all digits in non-decreasing order
// to ensure that the incrementing logic in the search loop works correctly.
// For example 223450 => 223455, 120568 => 122222 and 439999 => 444444.
if let Some(index) = digits.array_windows().position(|&[a, b]| a > b) {
let next = digits[index];
digits[index..].fill(next);
}
while digits <= end {
// Build a 5 bit binary mask with a `1` if two adjacent digits are equal.
let mask = digits.array_windows().fold(0, |acc, &[a, b]| (acc << 1) | (a == b) as u32);
// Password must contain at least one pair.
part_one += (mask != 0) as u32;
// Password must contain at least one pair that's not part of a larger group.
part_two += (mask & !(mask >> 1) & !(mask << 1) != 0) as u32;
// Find the next number with all digits in non-decreasing order.
let index = digits.iter().rposition(|&d| d < b'9').unwrap();
let next = digits[index] + 1;
digits[index..].fill(next);
}
(part_one, part_two)
}
pub fn part1(input: &Input) -> u32 {
input.0
}
pub fn part2(input: &Input) -> u32 {
input.1
}
fn to_digits(n: u32) -> [u8; 6] {
format!("{n:06}").into_bytes().try_into().unwrap()
}