A collection of algorithms for solving 2x2 and 3x3 Rubik's Cubes, including a vectorized implementation of Kociemba's two-phase algorithm.
I am using uv but you can use any Python environment of your choice.
git clone https://github.com/twaclaw/rubik.git
cd rubik
uv venv --python=3.13
source .venv/bin/activate
uv install -e .These algorithms can be invoked using the rubik command line tool and can be used to solve 2x2 cubes. They can also solve instances of the 3x3 cube that are not overly scrambled (i.e., rubik --help for a complete list of options.
The following algorithms are implemented and can be passed as an argument to the -a (--algorithm) option, for example rubik -a bi-bfs.
- Uninformed-search algorithms:
-
bfs$\to$ Breadth-First Search -
iddfs$\to$ Iterative Deepening Depth-First Search
-
- Meet-in-the-middle algorithms:
-
bi-bfs$\to$ Bidirectional Breadth-First Search -
bi-iddfs$\to$ Bidirectional Iterative Deepening Depth-First Search
-
# Solve a 2x2 cube that has been randomized with five scrambles using IDDFS.
rubik -a iddfs -n 5 -s 2
# Solve a specific 2x2 cube (defined by a string) using BFS
rubik -c BDBULRLRDBUBFDFURRLLFUFD -a bfs -s 2
# Solved a specific 3x3 cube using Bi-IDDFS
rubik -a bi-iddfs -c BLFFUDLUDBBUBRUFFFBRLFFUFLLRDDRDDRDRDRUULLBBDLBRLBFURU -s 3If there's anything more beautiful than an elegant algorithm, it's an elegant algorithm inspired by mathematics. Herbert Kociemba's two-phase algorithm is beautiful, elegant, and efficient.
The kociemba command-line tool implements Kociemba's algorithm. My implementation is a Numpy vectorized port of his original Python implementation. See src/rubik/kociemba for a complete list of the changes I made.
# Create the tables needed by the algorithm (only once)
kociemba --verbose --create-tables --path ./tables# Solve a random 3x3 cube scrambled with 20 random moves
kociemba solve -n 20
# Solve a specific cube defined by a string
kociemba solve -c BLFFUDLUDBBUBRUFFFBRLFFUFLLRDDRDDRDRDRUULLBBDLBRLBFURUFinally, if you specify the --video, --gif or --image options, a video (like the one above), GIF or image will be generated using Manim.
kociemba solve -c BLFFUDLUDBBUBRUFFFBRLFFUFLLRDDRDDRDRDRUULLBBDLBRLBFURU --video --image- Kociemba's home page
- Kociemba's GitHub
- cube20.org
- The code for the video rendering/3d animation was adapted from WampyCakes/manim-rubikscube.
