|
1 | 1 | //! # Infinite Elves and Infinite Houses |
2 | 2 | //! |
3 | | -//! The amount of presents that each house receives in part one is 10 times the |
| 3 | +//! ## Part One |
| 4 | +//! |
| 5 | +//! The amount of presents that each house receives is 10 times the |
4 | 6 | //! [divisor function](https://en.wikipedia.org/wiki/Divisor_function) `σ`. |
5 | 7 | //! For example the divisors of 6 are 1, 2, 3 and 6, so house 6 receives |
6 | 8 | //! 10 + 20 + 30 + 60 = 120 presents. |
7 | 9 | //! |
| 10 | +//! If `n` has the prime factorization `n = p₁^a₁ × p₂^a₂ × ... × pₖ^aₖ` then the sum of divisors is |
| 11 | +//! `σ(n) = [(p₁^(a₁+1) - 1)/(p₁ - 1)] × [(p₂^(a₂+1) - 1)/(p₂ - 1)] × ... × [(pₖ^(aₖ+1) - 1)/(pₖ - 1)]` |
| 12 | +//! or more compactly `σ(n) = ∏ᵢ₌₁ᵏ [(pᵢ^(aᵢ+1) - 1)/(pᵢ - 1)]` |
| 13 | +//! |
| 14 | +//! For example `n = 12 = 2² × 3¹` |
| 15 | +//! |
| 16 | +//! * `σ(12) = [(2³ - 1)/(2 - 1)] × [(3² - 1)/(3 - 1)]` |
| 17 | +//! * `[(8 - 1)/1] × [(9 - 1)/2] = 7 × 4 = 28` |
| 18 | +//! |
| 19 | +//! Starting from 41 (the largest possible prime encountered in houses up to 50 billion presents) |
| 20 | +//! we recursively try smaller and smaller prime powers, finding the smallest house number that |
| 21 | +//! exceeds the target. |
| 22 | +//! |
| 23 | +//! ## Part Two |
| 24 | +//! |
8 | 25 | //! It's highly likely that the answer will be a |
9 | 26 | //! [superabundant number](https://en.wikipedia.org/wiki/Superabundant_number) but there's no way |
10 | 27 | //! to easily *prove* that so we brute force check every possible solution. The approach is similar |
|
26 | 43 | //! We break the search range into fixed size blocks from `start` to `end`, |
27 | 44 | //! for example 200000 to 299999 with a block size of 100000. Then we can make some observations: |
28 | 45 | //! |
29 | | -//! * Elf 1 visits each house once. |
30 | 46 | //! * Elves from `start` to `end` each visit a different house exactly once, |
31 | 47 | //! bringing start, start + 1, ... presents. |
32 | 48 | //! * Elves from `end / 2` to `start` skip over all the houses. |
33 | 49 | //! * Elves from `block size` to `end / 2` visit *at most* one house as the increment is |
34 | 50 | //! greater than the size of the block. |
35 | 51 | //! * Elves from `2` to `block size` may visit any number of times. |
36 | | -
|
37 | | -// More explicit syntax fits in with surrounding code better. |
38 | | -#![allow(clippy::needless_range_loop)] |
39 | | - |
| 52 | +use crate::util::hash::*; |
40 | 53 | use crate::util::parse::*; |
41 | 54 |
|
| 55 | +/// Covers up to 50 billion presents for part one. The highest prime factor is 41. |
| 56 | +const PRIMES: [u32; 13] = [41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2]; |
| 57 | +/// Optimize part two by only searching within the fewest blocks. |
42 | 58 | const BLOCK: usize = 100_000; |
43 | | - |
44 | | -type Input = (u32, usize); |
45 | | - |
46 | | -pub fn parse(input: &str) -> Input { |
47 | | - let robins_inequality = [ |
48 | | - (100000, 4352000), |
49 | | - (200000, 8912250), |
50 | | - (300000, 13542990), |
51 | | - (400000, 18218000), |
52 | | - (500000, 22925240), |
53 | | - (600000, 27657740), |
54 | | - (700000, 32410980), |
55 | | - (800000, 37181790), |
56 | | - (900000, 41967820), |
57 | | - (1000000, 46767260), |
58 | | - (1100000, 51578680), |
59 | | - (1200000, 56400920), |
60 | | - (1300000, 61233020), |
61 | | - (1400000, 66074170), |
62 | | - (1500000, 70923680), |
63 | | - (1600000, 75780960), |
64 | | - (1700000, 80645490), |
65 | | - (1800000, 85516820), |
66 | | - (1900000, 90394550), |
67 | | - (2000000, 95278320), |
68 | | - ]; |
69 | | - |
70 | | - // Find a starting block closer to the answer. Part two presents overall are lower than |
71 | | - // part one so the answer will also be after this block. |
72 | | - let (target, mut start) = (input.unsigned(), 0); |
73 | | - |
74 | | - for (key, value) in robins_inequality { |
75 | | - if target >= value { |
76 | | - start = key; |
77 | | - } else { |
78 | | - break; |
79 | | - } |
80 | | - } |
81 | | - |
82 | | - assert!(target > 5040); |
83 | | - assert!(start > 100000); |
84 | | - |
85 | | - (target, start) |
| 59 | +/// Precomputed values for the first two million houses covering presents up to 95 million. |
| 60 | +const ROBINS_INEQUALITY: [(usize, u32); 20] = [ |
| 61 | + (100000, 4352000), |
| 62 | + (200000, 8912250), |
| 63 | + (300000, 13542990), |
| 64 | + (400000, 18218000), |
| 65 | + (500000, 22925240), |
| 66 | + (600000, 27657740), |
| 67 | + (700000, 32410980), |
| 68 | + (800000, 37181790), |
| 69 | + (900000, 41967820), |
| 70 | + (1000000, 46767260), |
| 71 | + (1100000, 51578680), |
| 72 | + (1200000, 56400920), |
| 73 | + (1300000, 61233020), |
| 74 | + (1400000, 66074170), |
| 75 | + (1500000, 70923680), |
| 76 | + (1600000, 75780960), |
| 77 | + (1700000, 80645490), |
| 78 | + (1800000, 85516820), |
| 79 | + (1900000, 90394550), |
| 80 | + (2000000, 95278320), |
| 81 | +]; |
| 82 | + |
| 83 | +pub fn parse(input: &str) -> u32 { |
| 84 | + input.unsigned() |
86 | 85 | } |
87 | 86 |
|
88 | | -pub fn part1(input: &Input) -> usize { |
89 | | - let (target, mut start) = *input; |
90 | | - let mut end = start + BLOCK; |
91 | | - let mut houses = vec![0; BLOCK]; |
92 | | - |
93 | | - loop { |
94 | | - // Elves that visit exactly once. |
95 | | - for i in 0..BLOCK { |
96 | | - houses[i] = 10 * (1 + start + i) as u32; |
| 87 | +pub fn part1(input: &u32) -> u32 { |
| 88 | + // Recursively compute the divisor sum greater than the target. |
| 89 | + fn divisor_sum(cache: &mut FastMap<(u32, u32), u32>, primes: &[u32], target: u32) -> u32 { |
| 90 | + if primes.is_empty() { |
| 91 | + return target; |
97 | 92 | } |
98 | 93 |
|
99 | | - // Elves that visit at most once. |
100 | | - for i in BLOCK..end / 2 { |
101 | | - let presents = 10 * i as u32; |
102 | | - let j = start.next_multiple_of(i) - start; |
103 | | - |
104 | | - if j < BLOCK { |
105 | | - houses[j] += presents; |
106 | | - } |
| 94 | + let key = (primes[0], target); |
| 95 | + if let Some(&value) = cache.get(&key) { |
| 96 | + return value; |
107 | 97 | } |
108 | 98 |
|
109 | | - // All remaining elves. |
110 | | - for i in 2..BLOCK { |
111 | | - let presents = 10 * i as u32; |
112 | | - let mut j = start.next_multiple_of(i) - start; |
| 99 | + // Try not including this prime. |
| 100 | + let mut result = divisor_sum(cache, &primes[1..], target); |
| 101 | + let mut power = 1; |
| 102 | + let mut sum = 1; |
113 | 103 |
|
114 | | - while j < BLOCK { |
115 | | - houses[j] += presents; |
116 | | - j += i; |
117 | | - } |
118 | | - } |
| 104 | + // Try increasing powers of this prime until the divisor sum exceeds the target. |
| 105 | + while sum < target { |
| 106 | + power *= primes[0]; |
| 107 | + sum += power; |
119 | 108 |
|
120 | | - if let Some(found) = houses.iter().position(|&p| p >= target) { |
121 | | - break start + found; |
| 109 | + let next = power * divisor_sum(cache, &primes[1..], target.div_ceil(sum)); |
| 110 | + result = result.min(next); |
122 | 111 | } |
123 | 112 |
|
124 | | - start += BLOCK; |
125 | | - end += BLOCK; |
| 113 | + cache.insert(key, result); |
| 114 | + result |
126 | 115 | } |
| 116 | + |
| 117 | + divisor_sum(&mut FastMap::new(), &PRIMES, input / 10) |
127 | 118 | } |
128 | 119 |
|
129 | | -pub fn part2(input: &Input) -> usize { |
130 | | - let (target, mut start) = *input; |
| 120 | +#[expect(clippy::needless_range_loop)] |
| 121 | +pub fn part2(input: &u32) -> usize { |
| 122 | + // Find a starting block closer to the answer, assuming we start from at least 100,000. |
| 123 | + // Part two presents overall are lower than part one so answer will also be after this block. |
| 124 | + let target = *input; |
| 125 | + let &(mut start, _) = ROBINS_INEQUALITY.iter().rfind(|&&(_, value)| target >= value).unwrap(); |
| 126 | + |
131 | 127 | let mut end = start + BLOCK; |
132 | 128 | let mut houses = vec![0; BLOCK]; |
133 | 129 |
|
|
0 commit comments