**From New Scientist #2903, 9th February 2013** [link]

I have several boxes, each containing a number of cubes. Each cube has black and white faces (at least one of each colour per cube), and each box contains all possible different cubes, with no duplicates. My three nephews opened the first box and each tried to assemble their own 2×2×2 cube with only one colour on its outer faces, and of course they failed. They then opened further boxes until they were each able to assemble a single-coloured 2×2×2 cube. They then put all the leftover cubes from the open boxes in a pile.

How many cubes were there in the leftover pile?

[enigma1735]

### Like this:

Like Loading...

The following Python program computes the number of different 2-coloured cubes in each box, and which of these can be used as vertices in the 2×2×2 cubes, then — assuming the three nephews construct two 2×2×2 cubes of one colour and one of the other colour — it calculates how many boxes are required, and how many of the smaller cubes remain. It’s not particularly elegant, but it does the job in 35ms.

Solution:There were 24 cubes left over.For sure not very elegant.

How long did it take to write the program, well written indeed.

Next time you may not use a nuclear weapon to solve such enigma

I wrote the program after solving the puzzle by hand, so the program is really just going through the steps I did manually – I didn’t want it to end up on my list of Enigmas that I’ve not solved programatically.

Once you’ve worked out which of the smaller cubes can be used to construct the larger cubes the solution is straightforward, so I decided to make it a more interesting programming challenge by deriving the rotations of the cube, and the set of different 2-colourings from scratch. I was hoping to come up with a neater way of generating all the rotations of the cube, but in the end – inspired by playing with a Rubik’s cube – I did it mechanically based on the rotations around each axis.

The program verifies the solution that I did manually, so I’m happy.

Thanks for your comment.

I downloaded your enigma.py library. Plenty of good tools.

I had an issue copying your solver for the 2x2x2 cube as some variables were not seen as global. I have still a lot to learn to use Python correctly.

Have a nice day

I’ve verified the code posted above (and the latest enigma.py library) runs on Python 2 and Python 3 (using 2.7.3 and 3.3.0 on OS X, 2.7.3 and 3.2.3 on Linux), so maybe there’s a problem with your Python installation? If you’re still having problems you could drop me an email with the error message your getting, but I’m not sure if I’ll be able to suggest anything as it all looks like it’s working at this end.

As I said previously, I have still a lot to learn. I was not my intention to suggest your program was wrong but rather to insist on the fact that I probably not run it as it shoul. Enjoy your day.