1use crate::util::iter::*;
7use crate::util::parse::*;
8
9type Input = (u32, u32);
10
11pub fn parse(input: &str) -> Input {
12 let [start, end] = input.iter_unsigned::<u32>().chunk::<2>().next().unwrap();
13
14 let mut digits = to_digits(start);
15 let end = to_digits(end);
16
17 let mut part_one = 0;
18 let mut part_two = 0;
19
20 if let Some(index) = digits.windows(2).position(|w| w[0] > w[1]) {
24 let next = digits[index];
25 digits[index..].fill(next);
26 }
27
28 while digits <= end {
29 let mask = digits.windows(2).fold(0, |acc, w| (acc << 1) | (w[0] == w[1]) as u32);
31
32 part_one += (mask != 0) as u32;
34 part_two += (mask & !(mask >> 1) & !(mask << 1) != 0) as u32;
36
37 let index = digits.iter().rposition(|&d| d < b'9').unwrap();
39 let next = digits[index] + 1;
40 digits[index..].fill(next);
41 }
42
43 (part_one, part_two)
44}
45
46pub fn part1(input: &Input) -> u32 {
47 input.0
48}
49
50pub fn part2(input: &Input) -> u32 {
51 input.1
52}
53
54fn to_digits(n: u32) -> [u8; 6] {
55 format!("{n:06}").into_bytes().try_into().unwrap()
56}