Day 25 - Moving fish

I've skipped a number of days, so I figured that I should take a punt at one of the more interesting ones.

In Day 25, we are modelling a number of fish, which can move either horizontally or vertically. They take turns, so we need to iterate through each of the fish one at a time, type by type, and move it to the new location.

We need to implement the algorithm correctly, we need to make each fish scan the space in front of them before we move them.

If we implement this this way, we are going to have to iterate over each list of animals twice. We can do this in a slightly more efficient way by building up a list of animals to move in the first pass, then only iterate through the animals that want to move.

We're going to build two main data structures therefore. We're going to have a pair of lookup tables, one that stores for each coordinate whether there is an animal in it. It's just a simple dictionary of coordinate->boolean. Secndly, we are going to have a pair of lists of east facing creates and a list of south facing creatures. Each creature in this list is a list of coordinates. When we look to move a creature, we look at the dictionary to see if the target location is filled. When we actually move, we're going to remoeve the old coordinates from the list, and add the new coordinates to teh list, as well as update the dictionary.

Ok, we can create a world with the data structures, so now we need to actually define a step command.

Ok, we can step over each stage, lets check that we can count interations until it becomes stable

Ok, lets get the puzzle input in and try that

And that's it, the end of AoC 2021. I'm going to try to tackle some of the challanges I missed over the next month or so in slow time, but that was fun.

I note that this one took nearly a minute to find a stable place. We could probably speed up the step function by using some slightly better structures, list appending is quite slow for example. But it works, and that's plently sufficient for now.