How I solved Mega Cube — Part 1 (Initial Victory)
Introduction
Once upon a time, when I was searching for a new hobby, I came across the Rubix cube. Back then I bought some of the Rubix cubes in different shapes, colors, and fashions (fashion ??!!! hmm. Anyway). I have started with the basic ones and gradually learned how to solve the harder ones. By the time that I got to Cube Twist Siamese Mirror Cube, life got busy. Everything got on hold and all the puzzles almost froze in the state that they were.
Fast forward to 6 years later, during the Covid lockdown, I felt it was time to get back to the old friends and tried to change some mood. That’s why I picked them where I left off. That is correct, solving Cube Twist Siamese Mirror Cube which I scrambled when I bought it. :(
As it is explained in this video, (and the Mirror in its name), it’s a combination of two modified 3x3x3 cubes. Why modified? Because some of the moves are not possible and we have a limited set of moves that we can use to solve this Modified cube. If I could find a solution for this Modified cube, then the big gigantic terrifying tower can be solved easily!!!
As some sort of practice, I have decided to use coding as a helping tool to solve it. Why? Because, why not! I was looking to expand my knowledge in Python, and it seemed a nice combination to expand my knowledge and solve an old friend puzzle.
As a developer, we are always interested to see if there is a similar project that can be adjusted so it can solve our problem. I have done some research and found some puzzle solver code. But all of them use the Object-oriented technique. Because of that and the method that they have used (transformation matrix for applying the moves), it’s not easy to adjust and define the Modified Cube moves. Those solutions are designed to solve the 3x3x3 cube and make any changes look very hard if not impossible.
Also recently I have been learning Functional programming and I was eager to use it in action.
To solve the Modified Cube, I assume that the fixed piece (the piece without movements) is located in the back and down the edge. If we do not rotate the cube, we can use the same terminology and the moves as a 3x3x3 cube but in a limited set of moves. If we accept that, the only remaining moves are “Up”, “Frond”, and “double layer Front”, and of course, their reversed moves.
Cubef V1
As I described before, I wanted to implement a solution to solve a simplified version of my Cube Twist Siamese Mirror Cube or I refer to it as the modified cube. My solution was to implement a representative of normal cube moves, and then, use those moves to model the modified cube. Although there are many ways to solve this puzzle, I have decided to use the A* (A_star) algorithm to solve the modified cube. To start, I modified one of the existed A* codes to generalize it enough to accept my functions as parameters. My implementation for the modified cube which I called Cubef can be found here.
This is a brief explanation for each file and its purpose:
- cube.py => Fundamental functions to define cube and different possible moves.
- Solver.py => Main code for preparing functions to call A*, and other Normal Cube move collections. It also has some helper functions to be used in A*.
- SolverModifiedCube.py => Modified Cube special moves collections.
- util.py => Main implementation of A*
- functional_helper.py => helper function for functional programming.
- example.py => Code example how to use Cubef
- t/ => Some unit tests
As a first version, I have copied an implementation of A* and made minimal changes to make it possible to pass my functions to it as the arguments. The V1 code is here.
The idea behind this implementation was to enable me to define the current state of the cube and specify the state that I wanted to transform to, and the solution provides the shortest number of moves for me.
The state of the cube has been defined as a string of char that represents the color of each piece. R for Red, G for Green, and so on. For each side, I have used a row-based flatten strategy. For the whole cube, I used this sequence, Top, Left, Front, Right, Back, and Bottom.
For example for the following cube (considering the Red side as Front), the sequence is as:
wob ryg oww rwy bbo gry gbr yrr ryo …
This representation is simple to change and reorder to apply the moves.
Initial Victories
This simple and concise solution brings huge initial victory to solve some of the strategy steps towards solving the Modified Cube. I have used the normal 3x3x3 strategy. I have solved the first layer, and second layer, and even the top layer cross using this implementation. When I wanted to put the last layer corner pieces in place, the code ran for weeks on my laptop and no result was provided.
Guess solution
To overcome the running time issue, I thought, adding some combined moves as a pattern, (e.g. UiFiUF or UFUiFi) might be helpful with A* speed up the process. Unfortunately, the result was not what I expected, and the code still was running for weeks with no result.