×

# Representing a Rubik's Cube

I've been working on a brilliant problem that requires programming a cube. Although, it seems that I've not reached the correct solution, I'll share my approach and discoveries.

Beware of bugs in the above code. I have only proved it correct but not tried it.

-Donald Knuth

Here is my code

Here are the things it define: a solved cube (Line 2 of the code), the various rotation operations of the cube(Line 47-58 of the code) and a function that counts the number of time(>0) an Algorithm must be applied on a solved cube to get back the solved cube itself(Line-60).

For example

countt([R,U,Rdash,Udash])


returns 6. This means that upon applying R U R' U' repeatedly 6 times on a solved cube, you get the solved cube back.

So, How does the code work?

Take a cube and hold it such that the White side is Up, and the Red side is Front. Now start numbering the top face with numbers from 0 to 8 columnwise. Now, hold the cube with your right hand and rotate the cube (and not the face) clockwise 180 Degrees such with your right hand such that the green is stll on your right. Now, number the down face (which is now held up) columnwise from 0 to 8 and also the back face(which is now held front) in the same way. Go back to the way as you held the cube initially. Look towards the right face from the right side of the cube. Number the sqares from 0 to 9 columnwise. Look towards the left face of the cube from the left and do the same thing.

We define the solved cube like this:

perfect_cube={'U':['W']*9,'F':['R']*9,'D':['Y']*9,'B':['O']*9,'L':['G']*9,'R':['B']*9}


,i.e, a dictionary consisting of the squares as a list. Note that the entries in the list are ordered as we numbered the squares for each cube.

Also, that is the basic data structure which is used to describe the cube everywhere throughout is this.

What about the rotations?

Every rotation has been split into two parts: Transposing the matrix of colors on top of the face and rotating the colors of the edges connected to it.

For example, consider doing the move U. First thing, we transpose the matrix on the top clockwise. Next, the squares on the top part on the right face come to the top part of the front, the squares of the front top go to the left and so on. In short, there is a cycle.

We make a copy of the old cube and build the new cube by replacing the corresponding parts from the new cube.

Just for an example, here is Line 51:

U = lambda c: fbmove('U',c,zip([0,3,6],[0,3,6],[8,5,2],[0,3,6]),['F','R','B','L'])


This means that replace the squares 0,3 and 6 of the front face with 0,3 and 6 of the right face.

and replace the squares 0,3 and 6 of the right face with 8,5 and 2 of the back face.

and replace the squares 8,5 and 2 of the back face with 0,3 and 6 of the Left face.

and replace the squares 0,3 and 6 of the Left face with 0,3 and 6 of the front face.

and ofcourse rotate the top squares by 90 Degree Clockwise.

The best way to understand what I am talking of is by actually having a cube with you.

I hope this guide has now shed some light on how to represent a Rubik's Cube through a program. Well, this is just a beginners way. I cannot guarantee that it is the optimal.

P. S.: Thanks to Thaddeus Abiy for correcting my code.

Note by Agnishom Chattopadhyay
3 years ago

Sort by:

I have replied to your dispute here .I Hope I cleared up any misunderstanding. · 3 years ago

Oh. Thanks. I'll review my code. By the way, that proble still has 0 solvers... · 3 years ago

Yeah ,hopefully you'll be the first. · 3 years ago

No, I lost all my 3 tries. I would not have Reported a problem unless I loose all my 3 tries. By the way, how much time does it take you to solve a cube? · 3 years ago

Too bad..I will add a solution soon or ill email you the code if you give me an adress.Im Not so fast( about 30seconds)..Im more interested in solving it on a computer.My classmate @Beakal Tiliksew can do it way faster,even blindfolded. · 3 years ago

30 seconds is good. It takes me around 80 seconds :( Please mail me the code at agnishom@gmail.com. I've not yet found any bug in Ddash... · 3 years ago

It took me about 15 years to accept the fact that I know how to do it but I cannot. · 2 years, 8 months ago

I have sent you the code :) · 3 years ago