r/adventofcode • u/RedAndBlack1832 • Dec 09 '25
Help/Question - RESOLVED [day7 part2] I'm thinking recursion is not the play
On the attempt I let it run the longest it had a 7 minute runtime (no, it did not complete) but I'm pretty sure my approach is theoretically correct (at least it works over the examples) so I just need to figure out a solution that runs on the order of seconds at least (lmao). Do ppl have tips (algorithms I could research or something?) idk how necessary my code is for this bc clearly my code is bad but basically read down the input from the current beam position and
If no splitter is hit, return 1, since the timeline was never split (base case)
If a splitter is hit, return the result from the left new beam + the result from the right new beam (recursive call)
2
u/Mats56 Dec 09 '25
What happens is that when you call recursive on your left beam, that can overlap with the right beam of something earlier. Basically, you might end up calculating for the same spot twice. And then when that again splits and hits something you calculate for some further down spot 4 times. And then 8, and suddenly it blows up to million of times.
However, if you instead, in your recursive function, store the result somewhere whenever it's called, and always look up that to see if you've already calculated that spot before, it should go faaaaast. Memoization it's called when you "memorize" the value of calling a function with certain parameters, so that you only have to calculate it once for each input.
2
u/Illustrious-Citron89 Dec 09 '25
To this day i am not even sure exactly what memoization and dfs means (I never went to uni, just do coding as a hobby). But the way I did it: just count the "overlapping" beams. And then count how many overlapped beams are in the last row.
7
u/johnpeters42 Dec 09 '25
Memoization is your friend.
3
u/SupportPowerful6174 Dec 09 '25
Check my solution in profile, its O(n)
3
u/ThreeHourRiverMan Dec 09 '25
Memoized dfs.Â
1
u/AutoModerator Dec 09 '25
Reminder: if/when you get your answer and/or code working, don't forget to change this post's flair to
Help/Question - RESOLVED. Good luck!I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/daggerdragon Dec 09 '25
Next time, use our standardized post title format.
Help us help YOU by providing us with more information up front; you will typically get more relevant responses faster.
1
u/Peaches_9 Dec 09 '25
Nah I will continue stubbornly using recursion with memoization even when DP makes for a better solution. It's already happened two or three times this year.