Skip to content

Instantly share code, notes, and snippets.

@LCamel
Created February 23, 2012 09:59
Show Gist options
  • Select an option

  • Save LCamel/1892034 to your computer and use it in GitHub Desktop.

Select an option

Save LCamel/1892034 to your computer and use it in GitHub Desktop.
Search
// http://www.facebook.com/groups/javascript.tw/194457013988978/
var n2ed = [ [ [0, 1], [1, 1] ], [ [0, 0], [1, 0], [2, 2], [3, 2] ], [ [2, 1], [3, 1] ] ];
function comp(val, edge) {
if (edge == 0) return val + 7;
if (edge == 1) return val / 2;
if (edge == 2) return val * 3;
if (edge == 3) return val - 5;
}
function go(node, val, prevEdge, depth) {
if (val == 2012 && node == 2)
return " ";
if (depth++ == 30)
return;
for (var i = 0; i < n2ed[node].length; i++) {
var edge = n2ed[node][i][0];
var dest = n2ed[node][i][1];
if (edge != prevEdge) {
var rtn = go(dest, comp(val, edge), edge, depth);
if (rtn)
return ['+7', '/2', '*3', '-5'][edge] + " " + rtn;
}
}
}
console.log(go(0, 2011, null, 0));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment