Fun With Coding: Rubik's Cube Challenge

4개월 전

While fiddling with a Rubik's Cube, I began to wonder if all I do is rotate the cube clockwise, then turn the right side clockwise, then repeat, could I get the cube back to its solved state? And how long would it take?

So I tried it...

animated gif

And I found out something interesting! After doing this 60 times, the top was solved! Also every 10 times, the cross on top came back. Interesting. I thought the pattern would be in multiples of 4 instead of 10.

The bottom though was not restored at all.

The Challenge

So I set out to see how many times it would take to get the bottom back. At first I was thinking 60 for the top and 60 tops for the bottom, so 3600. But that seemed a bit extreme - though not unheard of for Rubik's Cube complexity! But maybe 10 sets of 60 might do it. So I set out to try. With 2 - 3 seconds for each maneuver and 60 maneuvers per set, that's 2 - 3 minutes per set. With 10 sets that's...turning the cube for nearly a half hour straight.

I paced myself. Front was the same color (well, the center of it anyway) after each 60 set and the top was solved, so I could always know where I was. I got to 10 sets and...

It wasn't solved on the bottom.

That didn't deter me...well it did for a little while.

The Solution

But then I decided to look up how to code a Rubik's Cube!! Don't get too excited; it's another spreadsheet.

I'm actually enjoying coding on spreadsheets now. Since it's a visual thing and all I'm doing is moving colors around, I figured that's exactly what a spreadsheet does. I won't have to code color squares or be unpleasant and use text to represent colors (Y1, Y2 for yellow).

So back to Excel I go to write more macros!

First we need to set up the sheet to look like a flattened cube.

start view

Done. Oh, and throw some rules down while I'm at it.

Now to write some code. We need to map out our two moves:

  • Rotate the cube clockwise
  • Turn the right side clockwise

The advantage of using a spreadsheet here, is with embedded macros. These macros are just recorded actions from spreadsheet activity and translated to VBA code (Visual Basic for Applications). So you start recording the macro, move colors around to represent the move you're recording, then stop recording. In your macro viewer you will find all the code for that action. Here is the code for rotating the cube.

rotate

...and a bunch more of basically the same stuff: copy this cell and move it. It's quite long because I copy it all to a blank temporary cube placed below the main cube to make a cleaner transition: move to temporary space, make changes, move back. Turning the right side follows basically the same principle (including in length of code), so I won't post more of that.

Then to keep it tidy so there aren't black border lines running about, I clear and redraw the boxes.

boxes

There. One move done. 59 more to go to complete a set. Let's make a loop.

loop it

Here we repeat the process of rotating the cube, turning the right side, and doing a couple more things I haven't yet talked about that may or may nor be important... #foreshadowing

So there we are. I have this macro set to run 60 times when you press Ctrl-R. While watching I happened to see the bottom get solved in the middle of a run. I thought it might have been a fluke with refresh times on the sheet; shouldn't it not get solved until the top one does?

Then I added some color checker code. after each pass, it'll check to see if all bottom squares match. If so, declare "Match!" and say which pass number that was. I saw that it happened on the 13th run on pass number 36. So I started the whole thing over to see when the first one happened. This is what the result looked like.

match

The number 65535 represents Yellow and 16777215 represents White. It's the end of a run, so the top is solved. But you can see in the lower right that the bottom was solved on pass 12 of the 5th run. How many passes is that? 4 complete runs is 240, plus 12 is 252. Wait a minute, what's that other solved yellow? 12 completed runs is 720, plus another 36 passes is 756. 756 / 252 = 3, so it looks like we have a pattern. Now if there is a least common multiple we can find between 60 and 252, that should be where we may have it all solved.

And there is a multiple. 252 * 5 = 1260. And you know what? 60 * 21 = 1260 too. We should be able to have a fully solved cube in 21 runs. Let's try it!

Pass number 1259...

image.png

1260!!!!!!!! I did it!!

image.png

And boy are my Excel-hands tired! LOL!

I hope you enjoyed exploring this code/puzzle/spreadsheet challenge with me today. If you have any other math or coding problems, let me know! I'll see f I can find a solution!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
STEEMKR.COM IS SPONSORED BY
ADVERTISEMENT