This Python package contains two equivalent implementations (in C and Python) of Herbert Kociemba's two-phase algorithm for solving Rubik's Cube. Original Java implementation can be found here: http://kociemba.org/download.htm.
These ports are pretty straightforward (not to say dumb) and most probably can be optimized. But they have been extensively tested in our Rubik's cube solving machines (FAC System Solver and Meccano Rubik's Shrine), so be confident the algorithm is working.
NB please note that two-phase algorithm does not guarantee that the produced solution is the shortest possible. Instead, it gives you a "good enough" solution in a very short time. You can implement additional checks on top of this library, for example, to not produce any moves if the cube is already solved.
This package is published on PyPI and can be installed with:
$ pip install kociemba
It was tested under Python 2.7 and 3.3+.
You might need to install libffi system library beforehand. For example, on Debian-based distributions (e.g. Raspbian) you would run
sudo apt-get install libffi-dev.
Library should work on Windows, however it is not automatically tested at this moment: Travis CI doesn't have windows support.
pip install kociemba (or
pip3 install kociemba for Python 3.3+) should work, but you will need to install free build tools from Microsoft first. Check the following links:
The package exposes just one function
solve(), which accepts a cube definition string and returns a solution string in standard notation (see below).
Optional second argument allows solving to a specific pattern.
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" kociemba.solve('FLBUULFFLFDURRDBUBUUDDFFBRDDBLRDRFLLRLRULFUDRRBDBBBUFL', 'BBURUDBFUFFFRRFUUFLULUFUDLRRDBBDBDBLUDDFLLRRBRLLLBRDDF') u"R' D2 R' U2 R F2 D B2 U' R F' U R2 D L2 D' B2 R2 B2 U' B2"
When installing with pip,
kociemba will also register a command line tool with the same name. So you can also use it like this:
$ kociemba <cubestring>
The names of the facelet positions of the cube (letters stand for Up, Left, Front, Right, Back, and Down):
A cube definition string "UBL..." means that in position U1 we have the U-color, in position U2 we have the
B-color, in position U3 we have the L color etc. according to the order
So, for example, a definition of a solved cube would be
Solution string consists of space-separated parts, each of them represents a single move:
R U R’ U R U2 R’ U
C sources reside in the
ckociemba folder. Running
make inside this directory will compile a standalone binary. It accepts a cube representation as a command line argument, and writes the solution to the standard output. You can, of course, use
ckociemba sources directly in your projects.
kociemba will use C implementation under the hood. If something goes wrong (C version cannot be imported) it will automatically fall back to pure-Python implementation. However, it will be much slower.
To run the tests, clone the repository and run:
$ python setup.py test