Created
October 24, 2011 13:16
-
-
Save selman/1309005 to your computer and use it in GitHub Desktop.
Revisions
-
selman revised this gist
Oct 24, 2011 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -7,14 +7,14 @@ def solve alphametics alphametics.scan(/\w+/) do |w| firsts << w[0] rests += w[1..-1].chars end chars = firsts + rests fail "chars.size > 10" if chars.size > 10 size = firsts.size DIGITS.permutation(chars.size).each do |p| begin am = alphametics.dup chars.zip(p).each {|s, d| am.tr!(s, d)} -
selman revised this gist
Oct 24, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -24,4 +24,4 @@ def solve alphametics fail "unsolvable: #{alphametics}" end puts solve("mad * man == asylum") -
selman created this gist
Oct 24, 2011 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ require 'set' DIGITS = ('0'..'9').to_a def solve alphametics firsts, rests = Set.new, Set.new alphametics.scan(/\w+/) do |w| firsts << w[0] rests += w[1..-1].chars.to_set end chars = firsts + rests size = firsts.size fail "chars.size > 10" if chars.size > 10 DIGITS.permutation(chars.size).map do |p| begin am = alphametics.dup chars.zip(p).each {|s, d| am.tr!(s, d)} return am if eval(am) end unless p[0..size].include?('0') end fail "unsolvable: #{alphametics}" end puts solve("mad * man == as")