Skip to content
/ rubik Public

A collection of algorithms for solving 2x2 and 3x3 Rubik's Cubes, including a vectorized implementation of Kociemba's two-phase algorithm.

Notifications You must be signed in to change notification settings

twaclaw/rubik

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Magic Cube

A collection of algorithms for solving 2x2 and 3x3 Rubik's Cubes, including a vectorized implementation of Kociemba's two-phase algorithm.

Video

Rubik's Cube Solution Animation

Installation

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 .

Standard search algorithms

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., $n\le 10$). These algorithms are generally intractable for very scrambled 3x3 cubes. See 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.

Examples

# 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 3
📊 Click to expand: Output for the last command above

Kociemba's Two-Phase Algorithm

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

Examples

# Create the tables needed by the algorithm (only once)
kociemba --verbose --create-tables --path ./tables
📊 Click to expand: Output of the previous table generation


# 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 BLFFUDLUDBBUBRUFFFBRLFFUFLLRDDRDDRDRDRUULLBBDLBRLBFURU
📊 Click to expand: Output of the last command above


Finally, 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

Credits and Further Reading

About

A collection of algorithms for solving 2x2 and 3x3 Rubik's Cubes, including a vectorized implementation of Kociemba's two-phase algorithm.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages