r/adventofcode • • 3d ago

Tutorial [2016 Day 22 Part 2] Solving the problem in front of you

There's a bit of advice floating around that you don't have to solve the general case, only the problem in front of you. And, well, I've been working through past years, and 2016 Day 22 felt like a really good example that, so I thought I'd turn it into a tutorial about what that means.

In the general sense, you're given something similar to a 15 Puzzle. There's a grid of data nodes, you can transfer data between adjacent ones, and you need to get the data from the top right to the top left. Except... the numbers are a red herring. There are really only 4 numbers. Empty, small enough to fit onto any node, large enough to fit more or less anything, and massive enough to be a wall. The explanation for part 2 even calls attention to this by reducing the nodes to three states - empty, filled, and wall.

So already, my first instinct was to plot things and see where the walls are... and there was just a contiguous wall cutting across one of the rows, with everything else free. That makes things a lot easier.

If the empty square is already next to the data, you can just do loop-de-loops to slide it along. For example, from

..G_
....

it takes 4 moves to move the empty square around G to

._G.
....

it takes 1 move to shift G over

.G_.
....

and then it takes another 5 moves to shift it over by 1 again

G_..
....

So if I've already moved the empty space to the starting space for the data, the remaining moves are just 5*(cols - 2)... because the entire first 2 rows are free of walls. And all you have to do is use your pathfinding algorithm of choice to move the empty space there, then add that constant.

There are ways this could have been made more difficult. If there were walls on row 1, I would have had to do actual pathfinding for moving things along. If there were walls on row 0, I would have had to move the data in a more complicated path. If there were multiple blocks of data that needed moved around, it would have been more complex to optimize. But because the problem was nicely arranged to make things easier, I was able to streamline the solution.

3 Upvotes

1 comment sorted by

1

u/terje_wiig_mathisen 2d ago

I solved all of 2015-2018 after I had done parts of 2019 and then competed on a corporate leaderbord 2020-2022, this particular puzzle scared me so badly that I procrastinated (solving other years/days instead) for at least a month or two before I decided to see if there was a fast solution available, ignoring any nearly empty cells that could have been used to skip a regular 5-move operation.