r/adventofcode • u/rkek404 • 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.
8
u/[deleted] Dec 14 '21 edited Dec 14 '21
Let me try with a small prosaic writeup instead of code, because the code is straight forward once you understand the problem.
So for part2, the naive approach of just producing the string is unfeasible, because we end up with several petabytes of data because of the exponential growth. The string doubles each iteration.
Fortunately, we don't need to know the string to do the task, we only need to know the pairs we want to apply our transformation rules on.
So you already know that probably, because you used dictionaries to keep track of pairs. If we iterate this approach through until the end, we have one problem. Think about this string:
NNNC
If we track pairs only in a dictionary we would end up with something like {NN: 2, NC: 1}. The problem now is that we cannot guess in a straighforward fashion from the dict alone how many Ns we have. We miss some context for this.
The last missing piece for a solution is the question: how to reliably count occurences without having the whole string?
I hope this helps at least a little.
Edit: changed wording which was off
7
u/maikka39 Dec 14 '21
Just like in part 2 of day 6 (lanternfish), the trick is to not store the whole polymer in memory, but rather to keep track of how many times a combination occurs.
I did this the following way:
Firstly, I split the input into a variable containing the initial polymer, and a dictionary with the formulas for the new polymers.
Then I created another two dictionaries. One to keep track of the pairs, and one for the elements in the polymer.
The contents of these two variables would be the following for the sample:
By counting the pairs instead of putting them all in a string, we save a lot of memory and calculations.
With these two dictionaries, we can then go through all the days:
Here I loop over all the pairs, and split them into the pair itself, and how many times it occurs. I then retrieve the element that should be inserted in between from the formulas and save that into a variable.
After this, I add the amount of times the pair occurs to the total amount of elements of that type. (I.e., if
CHwould occur twice, we would add 2 to the counter forB).After incrementing the counter for the element, we can reset the counter for that pair and add the two new pairs to the list of pairs along with the amount of times they occur.
After all the days have passed, we can use the min and max of the counts for the elements to get our result:
I hope this has made it clear for you.
The full solution can be found here.
2
u/zopatista Dec 14 '21
I always write up how my approach works when committing my solutions and day 14 is no exception.
I used MathJax and markdown table syntax in my notebook which won't translate to Reddit, but here's the intro:
2
u/usefulthink Dec 14 '21
As others have noted, the key point is to count the polymer pairs instead of producing the complete string. So starting at the example given, you get
Note that if you now count just the first letter of each pair, you effectively count the letters in the entire-string, except for the very last letter.
Each pair is now expanded to two new pairs based on the rules:
This happens recursively, and you need to keep track of the total count of each pair. The expansion can be done based on the existing pairs instead of the resulting string, as 100 NN pairs will produce 100 NC and 100 CN pairs, no matter where they are. Counting the characters also works in this representation by summing up the counts of all pairs based on the starting-letter and adding the final letter of the input-string.
0
u/daggerdragon Dec 15 '21
In the future, please follow the submission guidelines by titling your post like so:
In doing so, you typically get more relevant responses faster.