Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for 8 Puzzle by htamas
def checkio(puzzle):
moves = ''
a, b = [(a, b) for a in range(3) for b in range(3) if puzzle[a][b] == 0][0]
while a < 2:
moves += 'D'
puzzle[a][b], puzzle[a+1][b] = puzzle[a+1][b], puzzle[a][b]
a += 1
while b < 2:
moves += 'R'
puzzle[a][b], puzzle[a][b+1] = puzzle[a][b+1], puzzle[a][b]
b += 1
snake = list(map({1: 0, 2: 1, 3: 2, 4: 5, 5: 4, 6: 3, 7: 6, 8: 7}.get,
[puzzle[0][0], puzzle[0][1], puzzle[0][2], puzzle[1][2],
puzzle[1][1], puzzle[1][0], puzzle[2][0], puzzle[2][1]]))
rotations = ['UULLDRURDLLURRDD', 'LUURDLDR', 'LLUURRDLULDDRR',
'ULLDRRULDLURRD', 'LULDRR', 'ULLDRURD']
for t in range(8):
for i in range(snake.index(t), t, -1):
if i == 7: i = 6
moves += rotations[i-1]
snake[i-1], snake[i], snake[i+1] = snake[i], snake[i+1], snake[i-1]
return moves
Oct. 22, 2013
Comments: