Had a little fun today at work with this little wooden cube puzzle. It consists of smaller alternating color cubes strung together in a way where the final result is a larger 3x3 cube when the smaller blocks are placed correctly.
Having had written a simple sudoku solver before using the recursive backtracking, I thought a solver could be written up fairly quickly using the same technique.
The way my solver works is by recusively trying to place the smaller 1x1 within a larger “virtual” 3x3 cube. It runs through each segment and places each block when it can, and if it can’t it’ll backtrack to the last successful call and continue trying to solve from there. Although it’s recursive it doesn’t run into any stack overflow problem because the size of the cube is quite small (total of 27 blocks inside), and the constraints are so limiting that it doesn’t travel too deep without seeing a problem.
I’ve made the code available on my github for anyone curious to see it, here.
And here’s the cube after being solved. :D