java: add solutions for year 2016, day 22 #115
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This puzzle was the kind where I had to actually study the input a bit, instead of just following the instructions. After I did that, the problem became much easier.
For part 2, there are three kinds of nodes:
This makes the grid a kind of graph, and we do not have to actually care about the numbers in the nodes -- but it is based on the assumption that any non-empty node (except for the immovable nodes) will be viable together with the empty node.
For the graph search, this is a simple BFS Dijkstra's algorithm, where the state consists of a tuple of two points: the position of the empty node, and the position of the data that we want to move. Then the empty node moves around, and if the empty node moves to the position of the data, they essentially swap places, moving the data. This is how the data is eventually moved to the node at
0, 0
.