11use memoize:: memoize;
2+ use num:: traits:: Euclid ;
23
34advent_of_code:: solution!( 11 ) ;
45
56type Stone = u64 ;
67type 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+
3338fn 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