So we’re nearing the end of April and I’m still (more or less) on schedule given the targets I set myself at the start of the year. In fact, I finished Day 11 before the deadline and so I took a little time to revisit Day 5 Part 2, and a few other bits of refactoring.
Day 11 This was a pretty easy puzzle, and one that took the classic AoC form of “Part 1: do this for n; Part 2: OK now do it for a bigger n”. The obvious solution for Part 1 takes way way way too long to run for Part 2. Essentially each step involves doing something to each element of a list. That something returns 0, 1 or 2 new items for the next iteration. So the size of the list can double each iteration if you put back in 2 elements each time. Doing this for 25 iterations is kind of manageable, you get a list of up 33 million-ish items for each starting item. (It’s actually less because you don’t always put 2 elements in the next list, but as a worst case estimate, this is ok). Part 2 is to do the same thing for 75 iterations which puts you up in the sextillions of items. The trick for Part 2 was to realise that order doesn’t matter, and all we ultimately care about is the final total number of items. So if there are duplicate numbers in your list, you can just do the calculation once and keep track of the number of duplicates. That means, instead of keeping track of a big list of items, you keep track of a smaller list of (item, repeats) pairs. My implementation meant re-sorting and re-grouping the list every iteration, but I’m sure there’s an even more efficient way of doing it.
Read more