Happy 40th anniversary Rubik's Cube!

Today the Rubik's cube celebrates 40 40 years of bringing joy to children and adults alike.Besides the pleasure of solving it,it also has a lot of mathematics to it too.

Consider a solved 3 × 3 3 \times 3 Rubik's cube. The six faces of the cube are named F R O N T FRONT , B A C K BACK , U P UP , D O W N DOWN , L E F T LEFT and R I G H T RIGHT respectively. An elementary move of the Rubik's cube is rotating a face by 90 ° 90° clockwise or 90 ° 90° anticlockwise. Any valid state of the Rubik's cube can be reached by applying these elementary operations one after the other.

An elementary move is denoted in the following fashion. If a given face is rotated by 90 ° 90° clockwise about the axis passing from the center of the face to the center of the cube, the move is denoted by the first letter of the name of the face. If the rotation is anticlockwise by 90 ° 90° , the letter is followed by an apostrophe (').These are all the elementary moves: R R U U L L D D B B F F R R' U U' L L' D D' B B' F F'

Given a single string of elementary moves S S let F ( s ) F(s) denote the number of times the sequence should be applied repeatedly to the solved cube to get back the original cube.

Consider the following ten strings D F D F' B R U F D B R' U' F' D' B F B B' F' B' F L U B F L' U B F B U L F B U L' D U F D U F F F F F' U B L U' B' L B R R U F B' R' R' U' F' B L B L B L' B L'

Find F ( S ) \sum F(S) for the ten strings above.

Explicit examples

F ( L B ) = 63 F(LB')=63

F ( R R ) = 1 F(RR')=1

F ( F U B ) = 90 F(FUB)=90


The answer is 1151.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Thaddeus Abiy
May 25, 2014

I was inspired to make this problem because me and a friend had coded a Rubik's cube solver program a while back. I had the cube implementation lying around for a while. It was made using object oriented programming. We have many versions, this one is long but it is the most readable.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import copy
CONVENTION = {'Y':('Top',0),'R':('Front',1),'B':('Left',2),
              'G':('Right',3),'W':('Bottom',4),'O':('Back',5)}

Solved = [[[i,i,i]]*3 for i in CONVENTION]

class Cube:    
    ''' A quick and dirty implementation of a Rubiks cube '''
    def __init__(self , Cube_Array , Faces = None ):
        '''Takes an array of faces and sorts them out and assigns a conventional reference point '''
        self.History = []
        if Faces == None:

            self.Faces  = {}
            for face in Cube_Array:
                Key = CONVENTION[ face[1][1] ][0]
                self.Faces[ Key ] = face        

        else:
            self.Faces = Faces

    def __repr__( self ):
        '''Output for printing the cube object'''
        for f in ['Front','Right','Back','Left','Top','Bottom']:
            for i in self.Faces[f]:print i
            print (f+'^=^')*12
        return ''

    def __getitem__(self,indice):
        return self.Faces[indice]

    def __iter__( self ):
        return iter( self.Faces.values() )

    def get_face( self , position ):
        '''Returns a face given the position of the'''
        return self.Faces[ position ]

    def get_row( self , position , row , Input = None):
        '''Returns a row of a given face'''
        if Input != None:return Input[ position ][ row ]
        return self.Faces[ position ][ row ]

    def get_column( self , position , column , Input = None):
        '''Return a column of a given face'''
        if Input != None:return [ Input[ position ][ row ][ column ] for row in range(3) ]
        column = [ self.Faces[ position ][ row ][ column ] for row in range(3) ]
        return column

    def is_solved(self):
        ''' Checks wheather the cube has been solved or not '''
        for face in self.Faces:
            All=self.Faces[face]
            if len(set(map(tuple,All))) != 1:
                return False
            for i in All:
                if len(set(i))!=1:
                    return False

        return True

    def F( self , clockwise = True ):
        faces = {i:[] for i in self.Faces}
        faces['Back'] = self.Faces['Back']
        for i in range(3):
            if clockwise:
                faces['Front'].append(self.get_column('Front',i)[::-1])
                faces['Right'].append([self.get_row('Top',2)[i]]+self.get_row('Right',i)[1::])
                faces['Left'].append(self.get_row('Left',i)[0:2]+[self.get_row('Bottom',0)[i]] )
                faces['Top'] = self.Faces['Top'][0:2] + [self.get_column('Left',2)[::-1]]
                faces['Bottom'] = [self.get_column('Right',0)[::-1]] + self.Faces['Bottom'][1::]
            else:
                faces['Front'].append(self.get_column('Front',2-i))
                faces['Right'].append([self.get_row('Bottom',0)[2-i]]+self.get_row('Right',i)[1::])
                faces['Left'].append(self.get_row('Left',i)[0:2]+[self.get_row('Top',2)[2-i]])
                faces['Top']=self.Faces['Top'][0:2] + [self.get_column('Right',0)]
                faces['Bottom']=[self.get_column('Left',2)]+self.Faces['Bottom'][1::]

        self.Faces = faces

    def Rotate_Up(self):
        ''' Rotate the whole cube 90 degrees up'''
        Faces = {i:[] for i in self.Faces}
        Faces['Top'] = self.Faces['Front']
        Faces['Front'] = self.Faces['Bottom']
        Faces['Back']=[ i[::-1] for i in self.Faces['Top'] ][::-1]
        Faces['Bottom'] =[ i[::-1] for i in self.Faces['Back']][::-1]
        for r in range(3):
            Faces['Right'].append( self.get_column('Right',r)[::-1] )
            Faces['Left'].append( self.get_column('Left',2-r))
        self.Faces = Faces

    def Rotate_Down(self):
        ''' Rotate the whole cube 90 degrees down'''
        for i in range(3):self.Rotate_Up()

    def Rotate_Right(self):
        ''' Rotate the whole cube 90 degrees to the right'''
        Faces = {i:[] for i in self.Faces}
        Faces['Right']=self.Faces['Front']
        Faces['Back']=self.Faces['Right']
        Faces['Left']=self.Faces['Back']
        Faces['Front']=self.Faces['Left']
        for r in range(3):
            Faces['Top'].append(self.get_column('Top',2-r))
            Faces['Bottom'].append(self.get_column('Bottom',r)[::-1])
        self.Faces=Faces

    def Rotate_Left(self):
        ''' Rotate the whole cube 90 degrees to the left'''
        self.Rotate_Right()
        self.Rotate_Right()
        self.Rotate_Right()

    def U(self,clockwise=True):
        ''' Rotate the upper face of the cube clockwise or anticlockwise'''
        TempCube = Cube( self.Faces.values() )      
        TempCube.Rotate_Down()
        TempCube.F(clockwise)
        TempCube.Rotate_Up()
        self.Faces = TempCube.Faces


    def D(self,clockwise=True):
        '''Rotate the bottom face of the cube clock/anti wise'''
        TempCube = Cube( self.Faces.values() )      
        TempCube.Rotate_Up()
        TempCube.F(clockwise)
        TempCube.Rotate_Down()
        self.Faces = copy.copy(TempCube.Faces)


    def B(self,clockwise=True):
        '''Rotate the back face of the cube clock/anti wise'''
        TempCube = Cube( self.Faces.values() )      
        TempCube.Rotate_Right()
        TempCube.Rotate_Right()
        TempCube.F(clockwise)
        TempCube.Rotate_Right()
        TempCube.Rotate_Right()
        self.Faces = TempCube.Faces


    def L(self,clockwise=True):
        ''' Rotate the left face of the cube clockwise or anticlockwise'''
        TempCube = Cube( self.Faces.values() )      
        TempCube.Rotate_Right()
        TempCube.F(clockwise)
        TempCube.Rotate_Left()
        self.Faces = TempCube.Faces


    def R(self,clockwise=True):
        ''' Rotate the right face of the cube clockwise or anticlockwise'''
        TempCube = Cube(  self.Faces.values() )     
        TempCube.Rotate_Left()
        TempCube.F(clockwise)
        TempCube.Rotate_Right()
        self.Faces = TempCube.Faces


    def Eval(self,moves):
        for move in moves:
            move += ' '
            Dir,Orientation = move[0],move[1]!="'"
            eval('self.%s(%s)' % (Dir,Orientation) )

A=Cube(Solved)
def F(S):
    New_Cube = copy.copy(A)
    count = 0
    while True:
        count += 1
        New_Cube.Eval(S)
        if New_Cube.is_solved():
            return count

Move_List = [["D","F'"],
             ['B',"R'","U'","F'","D'"],
             ["B","F'","B'"],
             ["F","L'","U","B"],
             ["F","B","U","L'"],
             ["D","U","F"],
             ["F","F'"],
             ["U'","B'","L"],
             ["B'","R'","R'","U'","F'"],
             ["B","L'","B","L'"]]

print sum(map(F,Move_List))

Nice solution! Thanks for adding it here

wow, much easier solution. Thanks :)

Vedabit Saha - 6 years, 10 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...