Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import copy
- class Rubik:
- """The cube."""
- def __init__(self):
- self.reset()
- def reset(self, cube = None):
- """Resets cube to solved one or given status."""
- if cube:
- self.cube = copy.deepcopy(cube) # independent copy of the given list. Helpful in bruteforce.
- else:
- self.cube = [ [["W", "W", "W"], ["W", "W", "W"], ["W", "W", "W"]], # six faces, in each face a 3x3 matrix.
- [["O", "O", "O"], ["O", "O", "O"], ["O", "O", "O"]],
- [["G", "G", "G"], ["G", "G", "G"], ["G", "G", "G"]],
- [["R", "R", "R"], ["R", "R", "R"], ["R", "R", "R"]],
- [["B", "B", "B"], ["B", "B", "B"], ["B", "B", "B"]],
- [["Y", "Y", "Y"], ["Y", "Y", "Y"], ["Y", "Y", "Y"]] ]
- def move_perm(self, perm, d=1):
- """Moves the cube given a permutation of pieces."""
- temp = []
- for x in perm:
- temp2 = []
- for y in x: temp2.append( self.cube[y[0]][y[1]][y[2]] )
- temp.append(temp2)
- for i in xrange(len(perm)):
- for j in xrange(len(perm[i])):
- self.cube[perm[i][j][0]][perm[i][j][1]][perm[i][j][2]] = temp[i][j-d]
- def move_sequence(self, seq):
- self.move_perm(seq.split())
- def move(self, face):
- """Moves a face of the cube."""
- d = 1
- if face[-1] == "'": d = 3
- elif face[-1] == "2": d = 2
- if face[0] == "U": self.move_perm(U_move, d)
- elif face[0] == "L": self.move_perm(L_move, d)
- elif face[0] == "F": self.move_perm(F_move, d)
- elif face[0] == "R": self.move_perm(R_move, d)
- elif face[0] == "B": self.move_perm(B_move, d)
- elif face[0] == "D": self.move_perm(D_move, d)
- def move_sequence(self, sequence):
- """Performs moves given a sequence of turns. Like scramble."""
- for turn in sequence.split(): self.move(turn)
- def show(self):
- for x in self.cube:
- for y in x: print y
- print
- def is_solved(self):
- for i in range(6):
- for j in range(3):
- for k in range(3):
- if self.cube[i][j][k] != self.cube[i][2][2]:
- return False
- return True
- # permutations OK
- U_move = [ [ [1, 0, 0], [4, 0, 0], [3, 0, 0], [2, 0, 0] ], [ [1, 0, 1], [4, 0, 1], [3, 0, 1], [2, 0, 1] ], [ [1, 0, 2], [4, 0, 2], [3, 0, 2], [2, 0, 2] ], [[0, 0, 0], [0, 0, 2], [0, 2, 2], [0, 2, 0]], [[0, 0, 1], [0, 1, 2], [0, 2, 1], [0, 1, 0]] ]
- L_move = [ [ [0, 0, 0], [2, 0, 0], [5, 0, 0], [4, 2, 2] ], [ [0, 1, 0], [2, 1, 0], [5, 1, 0], [4, 1, 2] ], [ [0, 2, 0], [2, 2, 0], [5, 2, 0], [4, 0, 2] ], [[1, 0, 0], [1, 0, 2], [1, 2, 2], [1, 2, 0]], [[1, 0, 1], [1, 1, 2], [1, 2, 1], [1, 1, 0]] ]
- F_move = [ [ [0, 2, 0], [3, 0, 0], [5, 0, 2], [1, 2, 2] ], [ [0, 2, 1], [3, 1, 0], [5, 0, 1], [1, 1, 2] ], [ [0, 2, 2], [3, 2, 0], [5, 0, 0], [1, 0, 2] ], [[2, 0, 0], [2, 0, 2], [2, 2, 2], [2, 2, 0]], [[2, 0, 1], [2, 1, 2], [2, 2, 1], [2, 1, 0]] ]
- R_move = [ [ [0, 2, 2], [4, 0, 0], [5, 2, 2], [2, 2, 2] ], [ [0, 1, 2], [4, 1, 0], [5, 1, 2], [2, 1, 2] ], [ [0, 0, 2], [4, 2, 0], [5, 0, 2], [2, 0, 2] ], [[3, 0, 0], [3, 0, 2], [3, 2, 2], [3, 2, 0]], [[3, 0, 1], [3, 1, 2], [3, 2, 1], [3, 1, 0]] ]
- B_move = [ [ [0, 0, 2], [1, 0, 0], [5, 2, 0], [3, 2, 2] ], [ [0, 0, 1], [1, 1, 0], [5, 2, 1], [3, 1, 2] ], [ [0, 0, 0], [1, 2, 0], [5, 2, 2], [3, 0, 2] ], [[4, 0, 0], [4, 0, 2], [4, 2, 2], [4, 2, 0]], [[4, 0, 1], [4, 1, 2], [4, 2, 1], [4, 1, 0]] ]
- D_move = [ [ [2, 2, 0], [3, 2, 0], [4, 2, 0], [1, 2, 0] ], [ [2, 2, 1], [3, 2, 1], [4, 2, 1], [1, 2, 1] ], [ [2, 2, 2], [3, 2, 2], [4, 2, 2], [1, 2, 2] ], [[5, 0, 0], [5, 0, 2], [5, 2, 2], [5, 2, 0]], [[5, 0, 1], [5, 1, 2], [5, 2, 1], [5, 1, 0]] ]
- def invert_move(face):
- if face[-1] == "2": return face
- if face[-1] == "'": return face[0]
- return face+"'"
- def invert_sequence(sequence):
- out = []
- for x in sequence.split()[::-1]:
- out.append(invert_move(x))
- return " ".join(out)
- def setup_corners(letter):
- out = []
- if letter == "B": out += ["R", "D'"]
- elif letter == "C": out += ["F", "R'"]
- elif letter == "D": out += ["F"]
- elif letter == "E": out += ["F'"]
- elif letter == "F": out += ["R'", "D'"]
- elif letter == "G": out += ["D", "F'"]
- elif letter == "H": out += ["R2", "F"]
- # elif letter == "I": out += []
- elif letter == "J": out += ["F2"]
- elif letter == "K": out += ["D2"]
- elif letter == "L": out += ["F2", "R'"]
- elif letter == "M": out += ["F'", "D"]
- elif letter == "N": out += ["F2", "D"]
- elif letter == "O": out += ["D"]
- elif letter == "P": out += ["R", "F"]
- elif letter == "Q": out += ["R'"]
- elif letter == "R": out += ["R2"]
- # elif letter == "S": out += []
- elif letter == "T": out += ["R"]
- elif letter == "U": out += ["R'", "F"]
- # elif letter == "V": out += []
- elif letter == "W": out += ["D'"]
- elif letter == "X": out += ["D'", "R"]
- return out
- def setup_edges(letter):
- out = []
- if letter == "A": out += ["R2", "U'", "R2"]
- # elif letter == "B": out += []
- elif letter == "D": out += ["R2", "U", "R2"]
- elif letter == "E": out += ["D'", "L2"]
- elif letter == "F": out += ["L2"]
- elif letter == "G": out += ["D2", "L2"]
- elif letter == "H": out += ["D", "L2"]
- elif letter == "I": out += ["L", "R", "F", "R", "U", "R2"]
- elif letter == "J": out += ["R'", "B'", "R'", "U'", "R2"]
- elif letter == "K": out += ["R", "F", "R", "U", "R2"]
- elif letter == "L": out += ["D", "R", "F", "R'", "L'"]
- elif letter == "M": out += ["R", "F'", "L'", "R'"]
- elif letter == "N": out += ["L'"]
- elif letter == "O": out += ["U2", "R", "U2"]
- elif letter == "P": out += ["R", "F", "L'", "R'"]
- # elif letter == "Q": out += []
- elif letter == "R": out += ["U'", "F'", "U"]
- elif letter == "S": out += ["U", "B", "U'"]
- elif letter == "T": out += ["R", "F'", "R", "U", "R2"]
- elif letter == "U": out += ["R'", "B", "L", "R"]
- elif letter == "V": out += ["U2", "R'", "U2"]
- elif letter == "W": out += ["L"]
- elif letter == "X": out += ["R'", "B'", "L", "R"]
- return out
- def next_edge(cube):
- letters = "ABCDIJKLMNOPQRSTUVWXEFGH"
- for i in xrange(6):
- face_color = cube.cube[i][1][1]
- if cube.cube[i][0][1] != face_color:
- if i != 3:
- return letters[4*i]
- if cube.cube[i][1][0] != face_color:
- return letters[4*i+1]
- if cube.cube[i][1][2] != face_color:
- if i != 0:
- return letters[4*i+2]
- if cube.cube[i][2][1] != face_color:
- return letters[4*i+3]
- return ""
- def edge(cube):
- """Current edge to throw."""
- c1 = cube.cube[0][1][2] # color1
- c2 = cube.cube[3][0][1] # color2
- if c1 == "W":
- if c2 == "B": return "A"
- if c2 == "O": return "B"
- if c2 == "R": return next_edge(cube)
- if c2 == "G": return "D"
- if c1 == "Y":
- if c2 == "G": return "E"
- if c2 == "O": return "F"
- if c2 == "R": return "G"
- if c2 == "B": return "H"
- if c1 == "O":
- if c2 == "W": return "I"
- if c2 == "B": return "J"
- if c2 == "G": return "K"
- if c2 == "Y": return "L"
- if c1 == "G":
- if c2 == "W": return "M"
- if c2 == "O": return "N"
- if c2 == "R": return "O"
- if c2 == "Y": return "P"
- if c1 == "R":
- if c2 == "W": return next_edge(cube)
- if c2 == "G": return "R"
- if c2 == "B": return "S"
- if c2 == "Y": return "T"
- if c1 == "B":
- if c2 == "W": return "U"
- if c2 == "R": return "V"
- if c2 == "O": return "W"
- if c2 == "Y": return "X"
- else: return ""
- def next_corner(cube):
- letters = "ABCDIJKLMNOPQRSTUVWXEFGH"
- for i in xrange(6):
- face_color = cube.cube[i][1][1]
- if cube.cube[i][0][0] != face_color:
- if i != 0 and i != 1:
- return letters[4*i]
- if cube.cube[i][0][2] != face_color:
- if i!=4:
- return letters[4*i+1]
- if cube.cube[i][2][0] != face_color:
- return letters[4*i+2]
- if cube.cube[i][2][2] != face_color:
- return letters[4*i+3]
- return ""
- def corner(cube):
- c1 = cube.cube[0][0][0]
- c2 = cube.cube[1][0][0]
- c3 = cube.cube[4][0][2]
- if c1 == "W":
- if c2 == "O" and c3 == "B": return next_corner(cube)
- if c2 == "B" and c3 == "R": return "B"
- if c2 == "G" and c3 == "O": return "C"
- if c2 == "R" and c3 == "G": return "D"
- if c1 == "Y":
- if c2 == "O" and c3 == "G": return "E"
- if c2 == "G" and c3 == "R": return "F"
- if c2 == "B" and c3 == "O": return "G"
- if c2 == "R" and c3 == "B": return "H"
- if c1 == "O":
- if c2 == "B" and c3 == "W": return next_corner(cube)
- if c2 == "W" and c3 == "G": return "J"
- if c2 == "Y" and c3 == "B": return "K"
- if c2 == "G" and c3 == "Y": return "L"
- if c1 == "G":
- if c2 == "O" and c3 == "W": return "M"
- if c2 == "W" and c3 == "R": return "N"
- if c2 == "Y" and c3 == "O": return "O"
- if c2 == "R" and c3 == "Y": return "P"
- if c1 == "R":
- if c2 == "G" and c3 == "W": return "Q"
- if c2 == "W" and c3 == "B": return "R"
- if c2 == "Y" and c3 == "G": return "S"
- if c2 == "B" and c3 == "Y": return "T"
- if c1 == "B":
- if c2 == "R" and c3 == "W": return "U"
- if c2 == "W" and c3 == "O": return next_corner(cube)
- if c2 == "Y" and c3 == "R": return "W"
- if c2 == "O" and c3 == "Y": return "X"
- return ""
- def old_pochmann(cube): # very bad list deal. split and join. need standard
- y_perm = "R U' R' U' R U R' F' R U R' U' R' F R" # corners
- t_perm = "R U R' U' R' F R2 U' R' U' R U R' F'" # edges
- solution = []
- while next_edge(cube) != "": # fix edges
- setup = " ".join(setup_edges(edge(cube)))
- temp = []
- temp += setup.split()
- temp += t_perm.split()
- temp += invert_sequence(setup).split()
- cube.move_sequence(" ".join(temp))
- solution += temp
- while next_corner(cube) != "": # fix corner
- setup = " ".join(setup_corners(corner(cube)))
- temp = []
- temp += setup.split()
- temp += y_perm.split()
- temp += invert_sequence(setup).split()
- cube.move_sequence(" ".join(temp))
- solution += temp
- return solution
- def program():
- sequence = "D' L2 B2 D F2 U2 F2 U R2 F2 R2 L' U' B D U2 R D2 F U'"
- cube = Rubik()
- cube.move_sequence(sequence)
- solution = " ".join(old_pochmann(cube))
- inverse = invert_sequence(solution)
- cube.reset()
- cube.move_sequence(inverse)
- count = 0
- while not cube.is_solved():
- cube.move_sequence(inverse)
- count += 1
- solution = inverse.split()*count
- print " ".join(solution)
- print len(solution)
- program()
Advertisement
Add Comment
Please, Sign In to add comment