Skip to content

Instantly share code, notes, and snippets.

@igrep
Created December 26, 2010 11:52
Show Gist options
  • Save igrep/755367 to your computer and use it in GitHub Desktop.
Save igrep/755367 to your computer and use it in GitHub Desktop.
#!/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