r/adventofcode • • Dec 14 '21

Help Day 14 Part 2 Algorithm

Day 14 part 1 was very easy, but Part 2 went right over my head, I tried using dictionaries to keep track of pairs, but my answers have off by one errors and it feels like guesswork - I don't full understand what I have written myself, like one of those times when it works but you don't know why. I know how people said it was basically lanternfish - but I never finished part 2 of lanternfish.

I would appreciate it if someone posted a little (detailed) writeup of day 14's part 2 in some easy to read language like python. I have gathered a lot from other's solutions but fail to understand any solution fully - even after reading multiple solutions I can't write one myself.

Others who are stuck might also benefit from this. Thanks in advance.

19 Upvotes

30 comments sorted by

View all comments

Show parent comment

2

u/zopatista Dec 14 '21 edited Dec 14 '21

No, because you only keep (unique number of pairs) counts. In the example that's 16 pairs, my puzzle input uses 10 elements so up to 100 unique pairs are possible. Keeping 100 counts doesn't take much RAM, even if you keep the counts for every step in memory (40 x 100 counts is still no more than 4000 counts).

Remember: the counts themselves only need a few bytes each; the largest count my puzzle input produced fits in 6 bytes. If you are using a language other than Python and need to specify your integer sizes, use uint64 or unsigned long long, at which point 4000 counts will take less than 32KiB of memory to store (not counting the memory for the pair names, 2 bytes each).