def test_solve_solves_solved_board(): board = board_from_string( """\ ...... ...... ....rr ...... ...... ...... """ ) eq_(solve(board), [])
def test_solve_simple_case(): board = board_from_string( """\ ...... ...... rr.... ...... ...... ...... """ ) eq_(solve(board), [(Car("r", Direction.horizontal, 2), Direction.horizontal, 4)])
def test_complex_test_case(): board = board_from_string( """\ ....AA ..BBCC rr..EF GGHHEF ...IEF ...IJJ """ ) moves = solve(board) for move in moves: board.move(*move) ok_(board.is_victory()) eq_(len(moves), 13)
def test_hardest_board(): # According to http://www.ulb.ac.be/di/algo/secollet/papers/crs06.pdf, # this is actually the hardest composition. # It is supposed to take 93 steps, but the solver solves it in 49. This is # due to a counting-difference in how slides across multiple squares is # counted (red car, 4 to the left can be counted as either 1 or 4 steps). board = board_from_string( """\ AAABCD EFFBCD E.rrCD HHI... .JI.KK .JLLMM """ ) moves = solve(board) ok_(_check_solves(board, moves)) eq_(len(moves), 49)
def test_first_level(): board = board_from_string( """\ .....A .....A .rr..A ...... ...... ...... """ ) moves = solve(board) ok_(_check_solves(board, moves)) eq_( moves, [ (Car("A", Direction.vertical, 3), Direction.vertical, 3), (Car("r", Direction.horizontal, 2), Direction.horizontal, 3), ], )
def _sign(v): return v / abs(v) if __name__ == "__main__": # Read the board if len(sys.argv) == 1: board_description = sys.stdin.read() else: with open(sys.argv[1]) as fp: board_description = fp.read() # Solve the board board = board_from_string(board_description) moves = solve(board) if moves is None: print("Board is not solvable. Sorry.") else: # Render the board for move in moves: direction_string = { (Direction.horizontal, 1): 'R', (Direction.horizontal, -1): 'L', (Direction.vertical, 1): 'D', (Direction.vertical, -1): 'U', }[move[1], _sign(move[2])] print("{}{}{}".format( move[0].color, direction_string,
def _check(lvl, board_desc, num_moves): board = board_from_string(board_desc) moves = solve(board) ok_(_check_solves(board, moves)) eq_(len(moves), num_moves)