# 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.

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.

7 years, 2 months ago

This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.

When posting on Brilliant:

• Use the emojis to react to an explanation, whether you're congratulating a job well done , or just really confused .
• Ask specific questions about the challenge or the steps in somebody's explanation. Well-posed questions can add a lot to the discussion, but posting "I don't understand!" doesn't help anyone.
• Try to contribute something new to the discussion, whether it is an extension, generalization or other idea related to the challenge.

MarkdownAppears as
*italics* or _italics_ italics
**bold** or __bold__ bold
- bulleted- list
• bulleted
• list
1. numbered2. list
1. numbered
2. list
Note: you must add a full line of space before and after lists for them to show up correctly
paragraph 1paragraph 2

paragraph 1

paragraph 2

[example link](https://brilliant.org)example link
> This is a quote
This is a quote
    # I indented these lines
# 4 spaces, and now they show
# up as a code block.

print "hello world"
# I indented these lines
# 4 spaces, and now they show
# up as a code block.

print "hello world"
MathAppears as
Remember to wrap math in $$ ... $$ or $ ... $ to ensure proper formatting.
2 \times 3 $2 \times 3$
2^{34} $2^{34}$
a_{i-1} $a_{i-1}$
\frac{2}{3} $\frac{2}{3}$
\sqrt{2} $\sqrt{2}$
\sum_{i=1}^3 $\sum_{i=1}^3$
\sin \theta $\sin \theta$
\boxed{123} $\boxed{123}$

Sort by:

I have replied to your dispute here .I Hope I cleared up any misunderstanding.

- 7 years, 2 months ago

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

- 7 years, 2 months ago

Yeah ,hopefully you'll be the first.

- 7 years, 2 months 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?

- 7 years, 2 months 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.

- 7 years, 2 months 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...

- 7 years, 2 months ago

I have sent you the code :)

- 7 years, 2 months ago

@Thaddeus Abiy, Elegant Code! Thanks. I corrected the bug in my program :)

- 7 years, 2 months ago

It took me about 15 years to accept the fact that I know how to do it but I cannot.

- 6 years, 10 months ago