Created
December 26, 2010 11:52
-
-
Save igrep/755367 to your computer and use it in GitHub Desktop.
SEND+MORE=MONEY in Ruby. ref: http://python.g.hatena.ne.jp/edvakf/20101225/1293305482
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# vim: set fileencoding=utf-8 : | |
$VERBOSE = true | |
require 'set' | |
def dig *args | |
args.inject{|x, y| 10 * x + y} | |
end | |
digits09 = (0..9).to_set | |
digits19 = (1..9).to_set | |
puts digits19.inject([]){|res, s| | |
s_ = [s] | |
(digits09 - s_).each{|e| | |
se = s_ + [e] | |
(digits09 - se).each{|n| | |
sen = se + [n] | |
(digits09 - sen).each{|d| | |
send = sen + [d] | |
send_i = dig( s, e, n, d ) | |
(digits19 - send).each{|m| | |
sendm = send + [m] | |
(digits09 - sendm).each{|o| | |
sendmo = sendm + [o] | |
(digits09 - sendmo).each{|r| | |
sendmor = sendmo + [r] | |
more = dig( m, o, r, e ) | |
(digits09 - sendmor).each{|y| | |
money = dig( m, o, n, e, y ) | |
res << "#{send_i} + #{more} = #{money}" if send_i + more == money | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
res | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment