Skip to content

Instantly share code, notes, and snippets.

@xionluhnis
Created August 29, 2022 18:43
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 xionluhnis/326f4618790dce6e8f578d7d428523d4 to your computer and use it in GitHub Desktop.
Save xionluhnis/326f4618790dce6e8f578d7d428523d4 to your computer and use it in GitHub Desktop.
Generating javascript programs from a set of 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