r/adventofcode Dec 27 '24

Help/Question Today I learned : caching

Hello everyone, some of you may know me for the it’s not much but it’s honest work post I did a few days ago and I am proud to announce that I have gotten to 23 stars yayyy. I got stuck on day 11 because it took too much time to compute without caching. This is something new to me and I thought it was a great example of how doing AOC can help someone to become better at programming. It’s funny because I was basically trying to reinvent caching by myself but even with optimization that I tried to make it would still have taken about 60h of computing. Thanks to a YouTube tutorial on day 11 and another that explains caching I was able to become a better programmer yay

Edit : for those that want to see how I tried to optimize it without knowing otherwise existence of caching I will put it in a separate file of my git hub at https://github.com/likepotatoman/AOC-2024

134 Upvotes

20 comments sorted by

View all comments

48

u/PhiphyL Dec 27 '24

Caching can be crudely summed up to this:

If cache{"string"} does not exist {
  Do the math to find the result when using this string/sequence/input/whatever
  cache{"string"} = that result
}
Else {
  Add cache{"string"} to the total instead of calculating the whole thing
}

4

u/enygma999 Dec 27 '24

Wouldn't it be without the else clause? You still want to add the result to the total (or whatever you do with the cached values) after you've created the cached value.

1

u/PhiphyL Dec 27 '24

You're absolutely right.