Skip to content

Instantly share code, notes, and snippets.

@ivolivares
Forked from aemkei/LICENSE.txt
Created June 3, 2014 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivolivares/099133511fcc46633f2f to your computer and use it in GitHub Desktop.
Save ivolivares/099133511fcc46633f2f to your computer and use it in GitHub Desktop.

Rubik's Pocket Cube Solver - 140byt.es

Solves Rubik's Pocket Cube (2x2x2) by using a simple brute force algorithm in only 100 bytes.

Demos:

You have to pass two movement instructions:

        TURN FRONT FACE CW:           ROTATE AROUND Y-AXIS:
          
          
              00 01                           01 03
              07 05                           00 02
                                              
      04 16   10 08   02 13           23 22   04 05   08 09
      06 17   11 09   03 15           21 20   06 07   10 11
                                      
              14 12                           18 16
              18 19                           19 17
                                              
              20 21                           15 14
              22 23                           13 12

This will solve the cube in less than a minute (~10.000.000 moves).

The same algorithm may be used to solve the original 3x3x3 cube, but it would take ages to find a solution.

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

function(
a, // input layout
b, // 1st movement
c, // 2nd movement
d, // placeholder
e // placeholder
){
for ( // cycle though all stickers
b = Math.random( // get random instruction
e = [ // create output layout
d = 24 // 6 faces * 4 stickers to check
]
) < .5 ? c : b; // reassign movement for later usage
d--;
e[d] = a[b[d]] // move sticker based on instruction
)
c |= a[d] ^ a[d&28]; // set "solved" flag
// if sticker's color on face do not match
return [ // return state
c, // ... "false" if solved
e // modified layout
]
}
function(a,b,c,d,e){for(b=Math.random(e=[d=24])<.5?c:b;d--;e[d]=a[b[d]])c|=a[d]^a[d&28];return[c,e]}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "rubik",
"description": "Solves a Rubik's Pocket Cube.",
"keywords": [
"rubik",
"pocket",
"cube",
"solve",
"algorithm"
]
}
<!DOCTYPE html>
<title>Rubik's Pocket Cube Solver - 140byt.es</title>
<style type="text/css" media="screen">
body {
font-family: Arial;
font-size: 12px;
padding: 10px;
}
span {
display: inline-block;
width: 20px;
height: 20px;
margin: -1px;
border: 1px solid black;
text-align: center;
line-height: 20px;
overflow: hidden;
vertical-align: top;
}
#output {
position: relative;
height: 260px;
}
#output div {
position: absolute;
}
#side_0 { top: 0; left: 44px; }
#side_1 { top: 44px; left: 0; }
#side_2 { top: 44px; left: 44px; }
#side_3 { top: 44px; left: 88px; }
#side_4 { top: 88px; left: 44px; }
#side_5 { top: 132px; left: 44px; }
#output div {
width: 44px; height: 44px;
}
</style>
<div id="output"></div>
<div id="counter"></div>
<a href="https://gist.github.com/1362710">Source Code</a>
<script>
var rubik = function(a,b,c,d,e){for(b=Math.random(e=[d=24])<.5?c:b;d--;e[d]=a[b[d]])c|=a[d]^a[d&28];return[c,e]},
turn = [01,03,00,02, 23,22,21,20, 04,05,06,07, 08,09,10,11, 18,16,19,17, 15,14,13,12],
spin = [00,01,07,05, 04,16,06,17, 10,08,11,09, 02,13,03,15, 14,12,18,19, 20,21,22,23],
scrambled = [0,1,5,4, 3,3,0,5, 4,3,0,1, 2,2,2,5, 1,4,5,4, 3,1,2,0],
output = document.getElementById("output"),
counter = document.getElementById("counter"),
start = new Date(),
count = 0,
solved = false,
input = scrambled,
loopStart,
interval;
function draw(data){
var div, span, value, color,
colors = ["red", "blue", "white", "green", "orange", "yellow"]
output.innerHTML = "";
for (var i=0;i<24;i++){
side = Math.floor(i/4);
color = data[i];
if (i%4 == 0){
div = document.createElement("div");
div.id = "side_" + side;
output.appendChild(div);
}
span = document.createElement("span");
span.innerHTML = color;
div.appendChild(span);
span.style.backgroundColor = colors[color];
}
counter.innerHTML = counter.innerHTML = count + " moves " +
"(" + ~~((loopStart - start)/1000) + " sec)";
}
function next(){
var state;
loopStart = new Date();
while (!solved && (new Date() - loopStart) < 1000){
state = rubik(input, turn, spin);
solved = !state[0];
if(!solved) {
count++;
input = state[1]
} else {
draw(input);
alert("solved");
}
}
if (!solved) {
draw(input);
interval = setTimeout(next, 1);
}
};
next();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment