Created
May 2, 2016 19:06
-
-
Save ConorOBrien-Foxx/1ea67ee9cd7f1e4219aee23c86423e2e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function output(x){ | |
console.log(x); | |
} | |
var funcs = [ | |
function(x,y){return x+y}, | |
function(x,y){return x-y}, | |
function(x,y){return x*y}, | |
function(x,y){return x/y}, | |
function(x){return x*x}, | |
function(x){return Math.sqrt(x)}, | |
function(x){return x+1}, | |
function(x){return x-1}, | |
function(x,y){return Math.pow(x,y)}, | |
function(x,y,z){return x?y:z}, | |
function(x,y){return Math.pow(x,y)}, | |
function(x){return String.fromCharCode(x)}, | |
function(x){return x.charCodeAt()}, | |
function(){return Math.E}, | |
function(){return Math.PI}, | |
function(){return 0}, | |
function(){return 1}, | |
function(){return 2}, | |
function(){return 3}, | |
function(){return 4}, | |
function(){return 5}, | |
function(){return 6}, | |
function(){return 7}, | |
function(){return 8}, | |
function(){return 9}, | |
function(){return 10}, | |
function(){return 11}, | |
function(){return 12}, | |
function(){return 13}, | |
function(){return 14}, | |
function(){return 15}, | |
function(){return 16}, | |
function(){return 50}, | |
function(){return 100}, | |
function(){return 1000}, | |
function(x){return new Function("a","b","c","d","e","return "+x)}, | |
function(x){return eval(x)}, | |
function(i,f){return this[i]=typeof f==="function"?f:new Function("return "+f)}, | |
function(){return +prompt()}, | |
function(){return prompt.charCodeAt()}, | |
function(x){return [x]}, | |
function(l){return this.splice(-l)}, | |
function(l){a=this.splice(-l);this.push.apply(this,a)}, | |
function(){return this[this.length-1];}, | |
function(x){output(String.fromCharCode(x))}, | |
function(x){output(String.fromCharCode(x));return x;} | |
]; | |
function steps(code){ | |
code = code.split("").map(function(e){return e.charCodeAt();}); | |
code = code.slice(0,-1).map(function(e,i){return Math.abs(code[i+1]-e);}); | |
return code; | |
} | |
function parse(code){ | |
code = steps(code); | |
var f = funcs.slice(); | |
var stack = []; | |
code.forEach(function(e){ | |
// console.log(stack); | |
var fp = f[e]; | |
var res = fp.apply(f,stack.splice(stack.length-fp.length,fp.length)); | |
if(typeof res !== "undefined" && res !== null) | |
stack.push(res); | |
}); | |
return stack; | |
} | |
// suboptimal solution | |
var nums = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,50,100]; | |
function aval(n){return nums.indexOf(n) >= 0;} | |
function genNum(n,k){ | |
k = k || []; | |
if(aval(n)){ | |
k.push(n); | |
return k; | |
} else if(nums.map(function(e){return n-e;}).some(aval)){ | |
for(var i=0;i<nums.length;i++) | |
if(aval(n-nums[i])){ | |
k = k.concat(genNum(n-nums[i])); | |
k.push(nums[i],"+"); | |
return k; | |
} | |
} else if(n > 0){ | |
for(var i=nums.length;i-->0;)if(n-nums[i]>=0)break; | |
k = k.concat(genNum(n-nums[i])); | |
k.push(nums[i],"+"); | |
return k; | |
} else return null; | |
} | |
// maps a series of instructions to differences | |
function mp(a){ | |
return a.map(function(e){ | |
return aval(e)?nums.indexOf(e)+15:e=="+"?0:-1; | |
}); | |
} | |
// suboptimal generation of source | |
function subBrute(ins){ | |
var counter = 33; | |
return String.fromCharCode(counter)+ins.map(function(e){ | |
return String.fromCharCode(counter += e); | |
}).join(""); | |
} | |
function bruteGenString(str){ | |
str = str.split(""); | |
return subBrute([].concat.apply([],str.map(function(e){ | |
return mp(genNum(e.charCodeAt()).concat(44)); | |
}))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment