···
1
+
use std::collections::HashSet;
8
+
const MAX_LENGTH: usize = 10;
let input: Vec<Range> = include_str!("../../input.txt")
···
22
-
let powers_of_ten: Vec<u64> = (0..10).map(|i| 10_u64.pow(i)).collect();
26
+
let powers_of_ten: Vec<u64> = (0..=MAX_LENGTH as u32).map(|i| 10_u64.pow(i)).collect();
let part_1 = input.iter().fold(0_u64, |acc, v| {
(v.start..=v.end).fold(acc, |acc, n| {
···
46
+
let mut part_2_invalid: HashSet<u64> = HashSet::new();
47
+
for len in 1..=MAX_LENGTH / 2 {
48
+
// 1-9, 10-99, 100-999, 100000-999999
50
+
*powers_of_ten.get(len - 1).unwrap_or(&1)..*powers_of_ten.get(len).unwrap()
53
+
// 0 is just the number (9), 1 is one repetition (99)
54
+
for repeat in 0..MAX_LENGTH / len {
55
+
number += combination * powers_of_ten.get((len * repeat) as usize).unwrap();
57
+
part_2_invalid.insert(number);
62
+
let part_2_invalid = part_2_invalid;
let part_2 = input.iter().fold(0_u64, |acc, v| {
43
-
(v.start..=v.end).fold(acc, |acc, n| {
65
+
(v.start..=v.end).fold(acc, |acc, num| {
47
-
let len = (n.ilog10() + 1) as usize;
48
-
match (1..=len / 2).rfind(|clen| {
49
-
let num = n % powers_of_ten.get(*clen).unwrap();
50
-
let res = (0..len / clen)
51
-
.fold(0, |acc, i| acc + num * powers_of_ten.get(i * clen).unwrap());
69
+
match part_2_invalid.get(&num) {
70
+
Some(_) => acc + num,