Skip to content

Instantly share code, notes, and snippets.

@fillano
Created February 23, 2012 11:13
Show Gist options
  • Save fillano/1892401 to your computer and use it in GitHub Desktop.
Save fillano/1892401 to your computer and use it in GitHub Desktop.
var input = 2011,
output = 2012,
depthLimit = 30,
countLimit = 100000;
count = 0,
depth = 0,
comp = [
function(x){return x+7},
function(x){return x/2},
function(x){return x*3},
function(x){return x-5;}
],
symbol = [
'+7 ',
'/2 ',
'*3 ',
'-5 '
],
path = [
[1,2,3],
[0,2,3],
[0,1,3],
[0,1,2]
],
results = [],
run=function (which,route,val) {
if(count>countLimit || depth>depthLimit || Math.round(val)!==val) {
return;
}
count++;
console.log(count + ':' + depth + ':' + val);
if(val===output) {
var next = [];
for(var i=0;i<route.length;i++)
next[i]=route[i];
next.push(which);
results.push(next);
}
route.push(which);
for(var i=0;i<path[which].length;i++) {
depth++;
if(path[which][i] !== which)
run(path[which][i], route, comp[which](val));
depth--;
}
route.pop();
}
run(0,[],input);
results.forEach(function(v) {
var str = input+' ';
v.forEach(function(i) {
str += symbol[i];
});
str += '= ' +output;
console.log(str);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment