Skip to content

Commit 1163c68

Browse files
committed
Use memoize cache buster
1 parent 93f796d commit 1163c68

File tree

3 files changed

+83
-8
lines changed

3 files changed

+83
-8
lines changed

Cargo.lock

Lines changed: 67 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ heapless = "0.8.0"
2929
itertools = "0.13.0"
3030
memoize = "0.4.2"
3131
mygrid = { version = "0.0.1", path = "mygrid" }
32+
num = "0.4.3"
3233
pico-args = "0.5.0"
3334
rayon = "1.10.0"
3435
regex = "1.11.1"

src/bin/11.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
use memoize::memoize;
2+
use num::traits::Euclid;
23

34
advent_of_code::solution!(11);
45

56
type Stone = u64;
67
type Step = u8;
78

89
#[memoize]
9-
fn blink_rec(stone: Stone, times: Step) -> u64 {
10+
fn blink_rec(_cache_buster: u64, stone: Stone, times: Step) -> u64 {
1011
if times == 0 {
1112
return 1;
1213
}
1314

1415
if stone == 0 {
15-
return blink_rec(1, times - 1);
16+
return blink_rec(_cache_buster, 1, times - 1);
1617
}
1718

1819
let mut digit_count = 0;
@@ -23,14 +24,22 @@ fn blink_rec(stone: Stone, times: Step) -> u64 {
2324
}
2425

2526
if digit_count % 2 == 0 {
26-
let divisor = 10_u64.pow(digit_count / 2);
27-
return blink_rec(stone / divisor, times - 1) + blink_rec(stone % divisor, times - 1);
27+
let divisor: u64 = 10_u64.pow(digit_count / 2);
28+
let (upper, lower) = stone.div_rem_euclid(&divisor);
29+
return blink_rec(_cache_buster, lower, times - 1)
30+
+ blink_rec(_cache_buster, upper, times - 1);
2831
}
2932

30-
return blink_rec(stone * 2024, times - 1);
33+
return blink_rec(_cache_buster, stone * 2024, times - 1);
3134
}
3235

36+
static mut GLOBAL_SEQ: u64 = 0;
37+
3338
fn solve(input: &str, times: Step) -> u64 {
39+
let cache_buster = unsafe { GLOBAL_SEQ };
40+
unsafe {
41+
GLOBAL_SEQ += 1;
42+
}
3443
let stones = input
3544
.split_whitespace()
3645
.filter(|p| !p.is_empty())
@@ -41,7 +50,7 @@ fn solve(input: &str, times: Step) -> u64 {
4150

4251
let count = stones
4352
.iter() // so fast that parallel is slower
44-
.map(|start_stone| blink_rec(*start_stone, times))
53+
.map(|start_stone| blink_rec(cache_buster, *start_stone, times))
4554
.sum::<u64>();
4655

4756
return count;

0 commit comments

Comments
 (0)