|
1 |
| -use memoize::memoize; |
2 |
| -use num::traits::Euclid; |
| 1 | +use rustc_hash::FxHashMap; |
3 | 2 |
|
4 | 3 | advent_of_code::solution!(11);
|
5 | 4 |
|
6 | 5 | type Stone = u64;
|
7 | 6 | type Step = u8;
|
8 | 7 |
|
9 |
| -#[memoize] |
10 |
| -// have a memoize cache buster to fix timing multiple runs |
11 |
| -fn blink_rec(_cache_buster: u64, stone: Stone, times: Step) -> u64 { |
12 |
| - if times == 0 { |
13 |
| - return 1; |
14 |
| - } |
| 8 | +const MAX_STONE_COUNT: usize = 5000; |
| 9 | +const MAX_DIGIT_COUNT: usize = 40; |
15 | 10 |
|
16 |
| - if stone == 0 { |
17 |
| - return blink_rec(_cache_buster, 1, times - 1); |
| 11 | +// take advantage of the fact that there is a limited number of stone numbers |
| 12 | +// found by @maneatingape: https://github.com/maneatingape/advent-of-code-rust/blob/main/src/year2024/day11.rs#L3 |
| 13 | +struct StoneIdxPool { |
| 14 | + count: usize, |
| 15 | + stone_num_to_stone_idx: FxHashMap<Stone, usize>, |
| 16 | + stone_idx_to_stone_num: [Stone; MAX_STONE_COUNT], |
| 17 | +} |
| 18 | +impl StoneIdxPool { |
| 19 | + #[inline] |
| 20 | + fn new() -> Self { |
| 21 | + Self { |
| 22 | + count: 0, |
| 23 | + stone_num_to_stone_idx: FxHashMap::default(), |
| 24 | + stone_idx_to_stone_num: [0; MAX_STONE_COUNT], |
| 25 | + } |
18 | 26 | }
|
19 | 27 |
|
20 |
| - let mut digit_count = 0; |
21 |
| - let mut temp = stone; |
22 |
| - while temp > 0 { |
23 |
| - digit_count += 1; |
24 |
| - temp /= 10; |
25 |
| - } |
26 |
| - // makes p1 2x slower but p2 0.2x faster |
27 |
| - // let digit_count = stone.checked_ilog10().unwrap_or(0); |
28 |
| - if digit_count % 2 == 0 { |
29 |
| - let divisor: u64 = 10_u64.pow(digit_count / 2); |
30 |
| - let (upper, lower) = stone.div_rem_euclid(&divisor); |
31 |
| - return blink_rec(_cache_buster, lower, times - 1) |
32 |
| - + blink_rec(_cache_buster, upper, times - 1); |
| 28 | + #[inline] |
| 29 | + fn get_idx(&mut self, stone_num: Stone) -> usize { |
| 30 | + if let Some(idx) = self.stone_num_to_stone_idx.get(&stone_num) { |
| 31 | + return *idx; |
| 32 | + } |
| 33 | + |
| 34 | + let idx = self.count; |
| 35 | + self.stone_idx_to_stone_num[idx] = stone_num; |
| 36 | + self.stone_num_to_stone_idx.insert(stone_num, idx); |
| 37 | + self.count += 1; |
| 38 | + idx |
33 | 39 | }
|
34 | 40 |
|
35 |
| - return blink_rec(_cache_buster, stone * 2024, times - 1); |
| 41 | + #[inline] |
| 42 | + fn get_stone_num(&self, idx: usize) -> Stone { |
| 43 | + self.stone_idx_to_stone_num[idx] |
| 44 | + } |
36 | 45 | }
|
37 | 46 |
|
38 |
| -static mut GLOBAL_SEQ: u64 = 0; |
39 |
| - |
40 | 47 | fn solve(input: &str, times: Step) -> u64 {
|
41 |
| - let cache_buster = unsafe { GLOBAL_SEQ }; |
42 |
| - unsafe { |
43 |
| - GLOBAL_SEQ += 1; |
| 48 | + let mut idx_pool = StoneIdxPool::new(); |
| 49 | + |
| 50 | + let mut divisors = [0; MAX_DIGIT_COUNT]; |
| 51 | + for i in 1..MAX_DIGIT_COUNT { |
| 52 | + divisors[i] = 10_u64.pow(i as u32 / 2); |
44 | 53 | }
|
45 |
| - let stones = input |
| 54 | + |
| 55 | + let mut stone_count: [Stone; MAX_STONE_COUNT] = [0; MAX_STONE_COUNT]; |
| 56 | + input |
46 | 57 | .split_whitespace()
|
47 | 58 | .filter(|p| !p.is_empty())
|
48 |
| - .map(|s| s.parse::<u64>()) |
| 59 | + .map(|s| s.parse::<Stone>()) |
49 | 60 | .filter(|r| r.is_ok())
|
50 | 61 | .map(|r| r.unwrap())
|
51 |
| - .collect::<Vec<_>>(); |
| 62 | + .for_each(|stone_num| { |
| 63 | + stone_count[idx_pool.get_idx(stone_num)] += 1; |
| 64 | + }); |
| 65 | + |
| 66 | + for _ in 0..times { |
| 67 | + let mut new_stone_count: [Stone; MAX_STONE_COUNT] = [0; MAX_STONE_COUNT]; |
| 68 | + stone_count |
| 69 | + .iter() |
| 70 | + .enumerate() |
| 71 | + .filter(|(_, &count)| count > 0) |
| 72 | + .for_each(|(stone_idx, &count)| { |
| 73 | + let stone_num = idx_pool.get_stone_num(stone_idx); |
| 74 | + if stone_num == 0 { |
| 75 | + new_stone_count[idx_pool.get_idx(1)] += count; |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + // this is a bit faster than ilog10 |
| 80 | + let mut digit_count = 0; |
| 81 | + let mut temp = stone_num; |
| 82 | + while temp > 0 { |
| 83 | + digit_count += 1; |
| 84 | + temp /= 10; |
| 85 | + } |
| 86 | + |
| 87 | + if digit_count % 2 == 0 { |
| 88 | + let divisor = divisors[digit_count]; |
| 89 | + new_stone_count[idx_pool.get_idx(stone_num / divisor)] += count; |
| 90 | + new_stone_count[idx_pool.get_idx(stone_num % divisor)] += count; |
52 | 91 |
|
53 |
| - let count = stones |
54 |
| - .iter() // so fast that parallel is slower |
55 |
| - .map(|start_stone| blink_rec(cache_buster, *start_stone, times)) |
56 |
| - .sum::<u64>(); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + new_stone_count[idx_pool.get_idx(stone_num * 2024)] += count; |
| 96 | + }); |
| 97 | + |
| 98 | + stone_count = new_stone_count; |
| 99 | + } |
57 | 100 |
|
58 |
| - return count; |
| 101 | + stone_count.iter().sum() |
59 | 102 | }
|
60 | 103 |
|
61 | 104 | pub fn part_one(input: &str) -> Option<u64> {
|
|
0 commit comments