r/dailyprogrammer • u/jnazario 2 0 • Oct 07 '16
[2016-10-07] Challenge #286 [Hard] Rush Hour Solver
Note The one I had posted earlier turns out to be a repeat one one earlier this year, so here's a fresh one.
Description
The game of Rush Hour is a puzzle game wherein the player has to slide the cars from their original position to allow the escape car to exit the board. Rush Hour is similar to other sliding puzzles, but with a twist: each piece moves along only one direction, instead of moving both horizontally and vertically. This makes individual moves easier to understand, and sequences easier to visualize. This is basically how cars move - forwards or backwards.
Rush Hour includes a 6x6 playing board with an exit opening along on edge, a red escape car, and several blocking cars (of dimensions 2x1) and several blocking trucks (of dimensions 3x1 ). The goal is to slide the red car (the escape vehicle) through the exit opening in the edge of the grid. To play, shift the cars and trucks up and down, left and right, until the path is cleared to slide the escape vehicle out the exit. You may not lift pieces off the grid. Pieces may only move forward and back, not sideways
In this challenge you'll be given a starting layout, then you have to show how to move the cars to allow the red escape car to exit the board.
Sample Input
You'll be given the 6x6 (or 7x7) board, indicating the exit (with a >), along with the red escape car (marked with an R), and blocking cars (2x1 sized, indicated with letters A through G) and trucks (3x1 sized, indicated with letters T through Z). Empty spaces will be marked with a .. The way the cars are facing should be obvious from their orientation. Remember, they can only move forwards or backwards. Example:
GAA..Y
G.V..Y
RRV..Y>
..VZZZ
....B.
WWW.B.Sample Output
Find a solution to the puzzle, preferably one with the minimal number of steps. You should indicate which cars move in which direction to liberate the red escape car (R). From our example above here's a solution with + indicating to the right or down N squares, - indicating to the left or up N squares (plus or minus relative to a 0,0 cell in the top left corner):
A +2
V -1
Z -1
Y +3
R +5Challenge Input
TTTAU.
...AU.
RR..UB>
CDDFFB
CEEG.H
VVVG.HChallenge Output
R +1
C -2
D -1
F -1
U +3
B -2
R +4Puzzles via the Rush Hour puzzle site.
4
u/den510 Oct 07 '16
For your sample I/O, wouldn't the 'VVV' still be in the way of the 'RR', because when 'VVV' shifts up, it leaves it's tail end in front of the 'RR' vehicle.
5
u/skeeto -9 8 Oct 07 '16 edited Oct 07 '16
C, representing the state of the puzzle as a set of bitmasks. It supports up to 8x8 and 32 vehicles. The solver is mostly brute force but finds the challenge solution in less than half a second. (
The sample input has no solution andthe provided sample solution is wrong.)Each vehicle gets a 64-bit integer representing the space it occupies. Another bit array keeps track of its direction. I slide it back and forth, checking for collisions against all vehicles in a single bitwise operation.
Challenge output:
3
u/thorwing Oct 09 '16 edited Oct 12 '16
JAVA 8 Edit: Made some changes that made the code more than twice as fast.
Pfff this was quite the challenge. Being a self-learned algorithm solver you sometimes don't see why certain things will not work until you try it. This time I could smack myself for my head with a baseball bat because guess what, at first I tried to solve it recursively! Let's just say that that took an enormous amount of time, so I scrapped that idea. Then I thought of a way to do it smartly and return the answer as fast as possible. Which works as follows.
Instantiate an insertion-ordered key-value pairmap with [key = grid] and [value = path]. And insert the initial grid with an empty path. And loop through that map in order.
Loop through all the possible moves (every car times both ways)
And that's basically it. This guarantees a best path as fast as possible due to the following things: The map grows in size but is basically ordered in amount of moves. Meaning that lower moves are calculate before. The first grid that maps to a new grid guarantees to be the best (or at least paired best). If another mapping then finds the same map, but that mapping already exists, it's certain that the old mapping was a better mapping and thus it keeps it. And so, the first grid that can calculate to a winning move, is the best move.
and Bonus output being:
A couple of things:
You can't actually map a 2D array as a key since it works with the .equals() method. So I stored them as a string and the reworked the string back into a map every time I needed to, this is frankly a waste of time, but the program was too fast to notice.Fixed this.2
u/stevarino Oct 11 '16
Python 3, aimed for readability. Just performing a breadth first search across valid moves for each state:
And the output:
2
u/gabyjunior 1 2 Oct 12 '16 edited Oct 12 '16
Solution in C, basically it runs a BFS from the initial grid playing legal moves until a solution is found. It is also storing grids already tested in a hash table to avoid retrying the same configuration more than once.
The source code is posted here along with the makefile and tested grids including sample/challenge, hard grids found by /u/thorwing and another hard grid that has a solution in 51 moves found in this document.
All puzzles are solved almost instantly (maximum of 25 ms for hard puzzles).
Maybe I will try to write a generator to test larger puzzles.
Output - Sample
Output - Challenge
Output - Hard 1
Output - Hard 2
Output - Hard 3
1
u/fulgen8 Oct 11 '16 edited Oct 11 '16
Recursive solution in Javascript, basically it tries to move the car on the starting position towards the destination, when another car is found blocking the path it tries to move that car to another position and returns to moving the previous car. It doesn't necessarily produce the solution with the minimal number of steps though.
Used matrix transpose to avoid having code to move vertically and horizontally.
EDIT: doesn't do well vs more complex puzzles
1
u/fulgen8 Oct 12 '16 edited Oct 12 '16
Another solution in Javascript, this one works well against complex puzzles (http://cs.ulb.ac.be/~fservais/rushhour/). It tries every possible one-square move on the puzzle each iteration until a solution is found.
challenge input: 140ms.
93 moves complex puzzle: 2.5 seconds.