Created
August 29, 2022 18:43
-
-
Save xionluhnis/326f4618790dce6e8f578d7d428523d4 to your computer and use it in GitHub Desktop.
Generating javascript programs from a set of characters
This file contains 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
"use strict" | |
const chars='+[]'.split('') | |
let pastStrings=[...chars] | |
const outs=[] | |
const num = 12 // number of characters to try appending | |
let best = null | |
mainLoop: | |
for(let i=0;i<num;++i){ | |
let newStrings=[] | |
for(const preStr of pastStrings){ | |
for(const c of chars){ | |
const newStr = preStr + c | |
newStrings.push(newStr) | |
try { | |
const val = eval(newStr) | |
outs.push([newStr, val]) | |
console.log(`${newStr} -> ${val}`) | |
if(val === 1){ | |
best = newStr | |
break mainLoop | |
} | |
} catch (err){ | |
} | |
} | |
} | |
pastStrings=newStrings | |
} | |
console.log(`${outs.length} valid program outputs`) | |
if(best){ | |
console.log('Found: ', best) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment