Advertisement
Jakube

Rubik's Cube Moves

Apr 4th, 2020
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. move_names = "RLUDFB"
  2.  
  3. def allow_adjacent_moves(move1, move2):
  4.     idx1 = move_names.index(move1[0])
  5.     idx2 = move_names.index(move2[0])
  6.     if idx1 // 2 != idx2 // 2:
  7.         # moves are on different axis
  8.         return True
  9.     if idx1 % 2 == 0 and idx2 % 2 == 1:
  10.         # moves are on same axis, but different and in correct order (RL, but not LR)
  11.         return True
  12.     return False
  13.  
  14. def recursive_gen(target_length, moves):
  15.     if len(moves) == target_length:
  16.         yield moves
  17.         return
  18.  
  19.     for move_name in move_names:
  20.         if moves and not allow_adjacent_moves(moves[-1], move_name):
  21.             continue
  22.  
  23.         for count in ("", "2", "'"):
  24.             moves.append(move_name + count)
  25.             yield from recursive_gen(target_length, moves)
  26.             moves.pop()
  27.  
  28.  
  29. if __name__ == '__main__':
  30.     for alg in recursive_gen(3, []):
  31.         print(' '.join(alg))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement