Enumerating all permutations of a Pocket Cube using Golang.

Braden Ripple
2 min readJul 12, 2022

--

If you just want to see the code it is here, https://sites.google.com/site/bradenrippleresume/pocket-cube-code

So, when it comes to solving a rubik’s cube, you generally have two types of people, sometimes 3 if you’re taking into to account a combination of the two types, and maybe 4 if you’re considering those who are not at all interested. Anyhow, semantics aside, the two types here are those who are into trying to solve it quickly by hand and maybe have some way of coming up with a quick solution versus those who are interested purely in the computational aspects of this problem and are looking for more optimal solutions if not the most optimal. I’d imagine it’s mostly this last type that might be interested here.

The one way I know of to come up with the most optimal solution for any rubik’s cube, is to enumerate all possibilities, which if you think about it makes sense because at each step you’re checking to see if each configuration has already been produced. The total number of enumerations, i.e. different ways a pocket cube can be rotated is (3⁷ * 8!)/24, reason being that there 8 factorial ways to position each of the eight cubes, 3⁷ ways to rotate each cube of the 7 cubes independent of the first placed cube which can be rotated 3 ways and positioned 8, making 24 different versions of the pocket cube. However, only one of those 24 will look like it’s solvable.

There perhaps some of you who are either don’t understand that last paragraph or don’t believe. That’s ok, This can be shown computationally.

But first, we have to consider running the code in itself. This code is in Golang, posted above. If you have some way to optimize this please send me a link or post it here, I would be more than willing to repost it. Initially writing this code in Python, then in Java, not able to use malloc in C++ and then using Golang, then trying to find a way to write this using matrix and tensor operations in Tensorflow, though the linear transformations in tensorflow are seemingly much more elegant, etc. etc., I’ve come to realize the nearly infinite ways one can beat this dead horse. The fastest I’ve been able to get this to run is in 35 seconds on a macbook pro.

This doesn’t generate the actual algorithm for each permutation, but could be modified to do so if you were to make an extra hashmap specifically for each individual set of rotations, i.e. adding to the rotations needed for each previous round of configurations.

--

--