SEND + MORE = MONEY: Difference between revisions
Content added Content deleted
(→{{header|Ruby}}: Add Ruby) |
|||
Line 1,077: | Line 1,077: | ||
SEND = 9567 MORE = 1085 MONEY = 10652 |
SEND = 9567 MORE = 1085 MONEY = 10652 |
||
done... |
done... |
||
</pre> |
|||
=={{header|Ruby}}== |
|||
Solving for the string "SEND + 1ORE == 1ONEY" using 'tr' , which translates characters to other characters. The resulting string is brutally evalled. |
|||
<syntaxhighlight lang="ruby">str = "SEND + 1ORE == 1ONEY" |
|||
digits = [0,2,3,4,5,6,7,8,9] # 1 is absent |
|||
uniq_chars = str.delete("^A-Z").chars.uniq.join |
|||
res = digits.permutation(uniq_chars.size).detect do |perm| |
|||
num_str = str.tr(uniq_chars, perm.join) |
|||
next if num_str.match?(/\b0/) #no words can start with 0 |
|||
eval num_str |
|||
end |
|||
puts str.tr(uniq_chars, res.join) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>9567 + 1085 == 10652 |
|||
</pre> |
</pre> |
||