fork download
  1. var sys = require('sys')
  2.  
  3. function combinations(xs, kk, func) {
  4. var ys = new Array()
  5. ys.length = kk
  6. combinationsAtInAfter(xs, 0, ys, -1, func)
  7. }
  8.  
  9. function combinationsAtInAfter(xs, jj, ys, nn, func) {
  10. for (var ii = nn+1; ii < xs.length; ii++) {
  11. ys[jj] = xs[ii]
  12. if (jj == ys.length-1) {
  13. func(ys)
  14. } else {
  15. combinationsAtInAfter(xs, jj+1, ys, ii, func)
  16. }
  17. }
  18. }
  19.  
  20. function permutations(xs, func) { permutationsStartingAt(xs.slice(0), 0, func) }
  21.  
  22. function permutationsStartingAt(xs, nn, func) {
  23. if (nn > xs.length-1) return xs
  24. if (nn == xs.length-1) return func(xs)
  25. var tmp
  26. for (var ii = nn; ii < xs.length; ii++) {
  27. tmp = xs[nn]; xs[nn] = xs[ii]; xs[ii] = tmp
  28. permutationsStartingAt(xs, nn+1, func)
  29. tmp = xs[nn]; xs[nn] = xs[ii]; xs[ii] = tmp
  30. }
  31. }
  32.  
  33. results = []
  34. var n1, n2, n3
  35.  
  36. function check(s,e,n,d,m,o,r,y) {
  37. return s > 0 && m > 0 &&
  38. (n1 = ((s*10+e)*10+n)*10+d)+
  39. (n2 = ((m*10+o)*10+r)*10+e)==
  40. (n3 = (((m*10+o)*10+n)*10+e)*10+y)
  41. }
  42.  
  43. combinations([0,1,2,3,4,5,6,7,8,9], check.length, function(comb) {
  44. permutations(comb, function(digits) {
  45. if (check.apply(this, digits)) results.push([["send", n1], ["more", n2], ["money", n3]])
  46. })
  47. })
  48.  
  49. results.forEach(function(x){ sys.print( x, "\n" ) })
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty