fork download
  1. console.time('SEND+MORE==MONEY')
  2. console.log( solve('SEND+MORE==MONEY') )
  3. console.timeEnd('SEND+MORE==MONEY')
  4.  
  5. console.time('third+third+third+seven==eight')
  6. console.log( solve('third+third+third+seven==eight') )
  7. console.timeEnd('third+third+third+seven==eight')
  8.  
  9.  
  10.  
  11. function solve ( Q ) {
  12. const words = Q.match(/[\w]+/g), ops = Q.match(/[^\w]+/g)
  13. const chars = [...new Set(words.join(''))], fchars = new Set(words.map(w=>w[0]))
  14.  
  15. let source = `(function(){\n`
  16. for ( let c of chars ) {
  17. source += `for (var ${c} = ${+fchars.has(c)}; ${c} <= 9; ${c}++) {\n`
  18. for ( let c2 of [...chars].splice( 0, chars.indexOf(c) ) ) {
  19. source += ` if (${c} == ${c2}) continue\n`
  20. }
  21. }
  22.  
  23. source += ` if ( (0 `
  24. for ( let [i, w] of words.entries() ) {
  25. for ( let [j, c] of [...w].reverse().entries() ) {
  26. source += `+ ${c} * 10e${j}`
  27. }
  28. if ( i != words.length - 1 ) source += `) ${ops[i]} (0 `
  29. }
  30. source += `) ) return [${chars.join(',')}]\n`
  31.  
  32. source += `${'}\n'.repeat( chars.length )}})()\n`
  33. const result = eval( source )
  34.  
  35. return result && Q.replace(/[\w]/g, c => result[chars.indexOf(c)])
  36. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty