var sys = require('sys')
function combinations(xs, kk, func) {
var ys = new Array()
ys.length = kk
combinationsAtInAfter(xs, 0, ys, -1, func)
}
function combinationsAtInAfter(xs, jj, ys, nn, func) {
for (var ii = nn+1; ii < xs.length; ii++) {
ys[jj] = xs[ii]
if (jj == ys.length-1) {
func(ys)
} else {
combinationsAtInAfter(xs, jj+1, ys, ii, func)
}
}
}
function permutations(xs, func) { permutationsStartingAt(xs.slice(0), 0, func) }
function permutationsStartingAt(xs, nn, func) {
if (nn > xs.length-1) return xs
if (nn == xs.length-1) return func(xs)
var tmp
for (var ii = nn; ii < xs.length; ii++) {
tmp = xs[nn]; xs[nn] = xs[ii]; xs[ii] = tmp
permutationsStartingAt(xs, nn+1, func)
tmp = xs[nn]; xs[nn] = xs[ii]; xs[ii] = tmp
}
}
results = []
var n1, n2, n3
function check(s,e,n,d,m,o,r,y) {
return s > 0 && m > 0 &&
(n1 = ((s*10+e)*10+n)*10+d)+
(n2 = ((m*10+o)*10+r)*10+e)==
(n3 = (((m*10+o)*10+n)*10+e)*10+y)
}
combinations([0,1,2,3,4,5,6,7,8,9], check.length, function(comb) {
permutations(comb, function(digits) {
if (check.apply(this, digits)) results.push([["send", n1], ["more", n2], ["money", n3]])
})
})
results.forEach(function(x){ sys.print( x, "\n" ) })
dmFyIHN5cyA9IHJlcXVpcmUoJ3N5cycpCgpmdW5jdGlvbiBjb21iaW5hdGlvbnMoeHMsIGtrLCBmdW5jKSB7CiAgIHZhciB5cyA9IG5ldyBBcnJheSgpCiAgIHlzLmxlbmd0aCA9IGtrCiAgIGNvbWJpbmF0aW9uc0F0SW5BZnRlcih4cywgMCwgeXMsIC0xLCBmdW5jKQp9CgpmdW5jdGlvbiBjb21iaW5hdGlvbnNBdEluQWZ0ZXIoeHMsIGpqLCB5cywgbm4sIGZ1bmMpIHsKICAgZm9yICh2YXIgaWkgPSBubisxOyBpaSA8IHhzLmxlbmd0aDsgaWkrKykgewogICAgICB5c1tqal0gPSB4c1tpaV0KICAgICAg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