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