This article introduces the library kociemba that calculates the solution of the Rubik's cube and explains how to use it easily.
There are various libraries in python, from libraries for drawing 3D graphs to mahjong libraries. Introducing this time is a library that instantly calculates the solution method for aligning the entire surface from the current state of the Rubik's cube.
I wrote this article so that those who are looking for a Rubik's Cube solver in university classes and free study during summer vacation can find kociemba immediately.
Thanks to this library, I was able to create a "robot that solves the Rubik's cube fully automatically". Video: Automatic Rubik's Cube Solver
Originally, it is the name of the scholar who invented the Kociemba's algorithm, which solves the Rubik's cube. His real name is Herbert Kociemba.
I was looking for a library to solve the Rubik's Cube for my department's production tasks. Actually, there are many other libraries for solving the Rubik's Cube, but many of them are extremely slow or I have no idea how to use them, so I had a hard time not knowing which one to use at that time. Meanwhile, I decided to use this library because it was the easiest to use and the calculation was quickest I tried.
My environment was Ubuntu 16.04 LTS. You can enter it with pip. It seems that it has been confirmed with Python2.7, Python3.3 +
pip install kociemba
Those who use Unix-based OS such as mac and ubuntu also need the following command.
sudo apt-get install libffi-dev
Use it as follows. The meaning of the symbols will be explained later.
python
>>> import kociemba
>>> kociemba.solve('DRLUUBFBRBLURRLRUBLRDDFDLFUFUFFDBRDUBRUFLLFDDBFLUBLRBD')
u"D2 R' D' F2 B D R2 D2 R' F2 D' F2 U' B2 L2 U2 D R2 U"
It is common sense for those who can make a Rubik's cube, but the axis of the Rubik's cube does not change no matter how many screws are turned. In the image below, you can see that the square in the center of each surface does not move no matter how you turn the surface.
By using this fact, we can see that even the complicated "Rubik's cube state" can be represented by a symbol that is uniquely determined.
Since it was found that the center cell of each surface does not move, it is now possible to specify the surface like "turn the surface whose center is red" no matter how disjointed it is.
In kociemba, the rotation angle is expressed as follows. R is the Red side,
R:90 degrees clockwise
R':90 degrees counterclockwise
R2:180 degree rotation
kociemba has each face instead of color (F (front), B (back), R (right), L (left), U (up), B (bottom)) so that it can be solved in any direction. It is represented by. Therefore, first, determine the correspondence between these symbols and the color of your Rubik's cube as follows. Naturally, the correspondence is decided by the color of the center of the surface facing the front.
--F (front) → white --B (back) → yellow
Next, we will investigate which color is in which position based on the following development drawing (this is extremely difficult).
For example, if there is yellow at the top of ʻU1, then ʻU1 = B
.
Finally, ʻU1, U2, U3, U4, U5, U6, U7, U8, U9, R1, R2, R3, R4, R5, R6, R7, R8, R9, F1, F2, F3, F4, F5, F6, F7, F8, F9, D1, D2, D3, D4, D5, D6, D7, D8, D9, L1, L2, L3, L4, L5, L6, L7, L8, L9, B1, B2, B3, Create the string str
in the order of B4, B5, B6, B7, B8, B9. and execute
kociemba.solve (str) `to get the solution.
Recommended Posts