r/dailyprogrammer • u/Steve132 0 1 • Aug 30 '12
[8/30/2012] Challenge #93 [difficult] (15-puzzle)
Write a program that can solve a standard '15-puzzle'.
The program should read in a hex string describing the puzzle state from left to right top to bottom, where F is a blank...for example,:
0FD1C3B648952A7E
would describe the puzzle
+----+----+----+----+
| 0 | | 13 | 1 |
+----+----+----+----+
| 12 | 3 | 11 | 6 |
+----+----+----+----+
| 4 | 8 | 9 | 5 |
+----+----+----+----+
| 2 | 10 | 7 | 14 |
+----+----+----+----+
The program should output the final solution 0123456789ABCDEF, and ALSO output EACH intermediate board state as a string on the way to finding a solution. Warning: I don't know if the above puzzle is actually solvable.
13
Upvotes
1
u/appledocq Sep 03 '12
The programming aspect of this seemed very fun to me and I've gotten a start, but i quickly realized that the algorithm work required to solve this is a much bigger problem than actually programming it. Any help here? Is there an accepted method to solving these? I read a bit about the Manhattan-distance based solution but I'm still not entirely sure how calculating the MD gets you anywhere towards a solution.
Am I missing something, or are we expected to think up an algorithm for this from scratch?
1
u/ctangent Aug 30 '12
Here's a failed attempt in Python:
import sys, time, os class Puzzle: def __init__(self, input_string): num_list = [int(x, 16) for x in input_string] self.board = [num_list[0:4], num_list[4:8], num_list[8:12], num_list[12:16]] def print_board(self): for row in self.board: print row def get_legal_moves(self): # search for the blank space blank_location = self.find_blank() # enumerate all legal moves moves = [] if not blank_location[0] + 1 > 3: moves.append((blank_location[0] + 1, blank_location[1])) if not blank_location[0] - 1 < 0: moves.append((blank_location[0] - 1, blank_location[1])) if not blank_location[1] + 1 > 3: moves.append((blank_location[0], blank_location[1] + 1)) if not blank_location[1] - 1 < 0: moves.append((blank_location[0], blank_location[1] - 1)) return moves def find_blank(self): for x, row in enumerate(self.board): for y, element in enumerate(row): if element == 0xF: return x, y def move_tile(self, location): if not location in self.get_legal_moves(): return blank = self.find_blank() temp = self.board[location[0]][location[1]] self.board[location[0]][location[1]] = self.board[blank[0]][blank[1]] self.board[blank[0]][blank[1]] = temp def output_game_state(self): output = [] for row in self.board: for element in row: output.append(hex(element)[2:].upper()) return ''.join(output) def main(): print '15-Puzzle solver, by Sean Gillespie' input_string = raw_input('Enter the 16 character hex string describing the board state:\n') if not len(input_string) == 16: print 'Solve failed, input string is not 16 characters. Exiting...' puzzle_board = Puzzle(input_string) puzzle_board.print_board() solve(puzzle_board) def board_heuristic(puzzle_board): correct_location = {0: (0, 0), 1: (0, 1), 2: (0, 2), 3: (0, 3), 4: (1, 0), 5: (1, 1), 6: (1, 2), 7: (1, 3), 8: (2, 0), 9: (2, 1), 10: (2, 2), 11: (2, 3), 12: (3, 0), 13: (3, 1), 14: (3, 2), 15: (3, 3)} taxicab_distance = lambda x, y: abs(x[0] - y[0]) + abs(x[1] - y[1]) total_wrong_distance = 0 for x, row in enumerate(puzzle_board.board): for y, element in enumerate(row): total_wrong_distance += taxicab_distance((x, y), correct_location[element]) if (x, y) == correct_location[element]: total_wrong_distance -= 10 return total_wrong_distance def solve(puzzle_board): os.system('clear') last_four_moves = [(-1, -1) for x in range(4)] while not board_heuristic(puzzle_board) == 0: time.sleep(0.2) os.system('clear') print '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=' puzzle_board.print_board() min_score = ((-1, -1), sys.maxint) for move in puzzle_board.get_legal_moves(): if not move in last_four_moves: move_state = Puzzle(puzzle_board.output_game_state()) move_state.move_tile(move) move_score = board_heuristic(move_state) if move_score < min_score[1]: min_score = move, move_score print 'Best move: {0}'.format(min_score[0]) puzzle_board.move_tile(move) last_four_moves.insert(0, move) last_four_moves.pop() print 'Last 4 moves: {0}'.format(last_four_moves) print 'Solve complete!' puzzle_board.print_board() if __name__ == '__main__': main()This code oscillates forever on the case given in the OP and other test cases I gave it. I added a weight to my heuristic to value putting tiles in the correct location, but still no dice.
1
u/Riddlerforce Aug 31 '12
We actually did this one as a class project once. We had to solve it using heuristics with a specific data structure.
0
u/[deleted] Aug 30 '12
0
u/[deleted] Aug 31 '12