in reply to Words that equal numbers

I would say that the first thing to notice is that you can constrain the numbers from the right hand end, for example if you have defined D to be 3 and S to be 4 you know that T must be 7. In fact we know that (D+S)%10 == T, (ND+ES)%100 == NT and so on. So if we had some code that assigns values in that order and checks constraints at the earliest moment it would solve the problem. To show you what I mean suppose we had some code:

# Note the digits that are not yet assigned $un0 = "0123456789"; foreach $d (split(//,$un0)) { my $un1 = $un0; $un1 =~ s/$d//; foreach $s (split(//,$un1)) { my $un2 = $un1; $un2 =~ s/$s//; foreach $t (split(//,$un2)) { # Now we can check the first constraint next unless(($d + $s)%10 == $t); my $un3 = $un2; $un3 =~ s/$t//; foreach $n (split(//,$un3)) { my $un4 = $un3; $un4 =~ s/$n//; foreach $e (split(//,$un4)) { next unless(($n.$d + $e.$s)%100 == $n.$t); . . . # Having assigned everything see if # we have the correct answer if(($j.$a.$m.$e.$s + $b.$o.$n.$d) \ == ($a.$g.$e.$n.$t)) { print "B=$b A=$a ... goto success; } } } } } } print "Failed\n"; exit(0); success: print "Solved\n"; # Warning code not checked in any way!

Of course the next step is is to convert the problem into a similar piece of text (without the comments and strange indentation). Once we have the text we can evaluate it and get the answer. (I'll leave the fine details to you)

Replies are listed 'Best First'.
Re: Re: Words that equal numbers
by tall_man (Parson) on Jan 19, 2003 at 00:31 UTC
    Your solution will work, hawtin (I tried it for the famous "SEND + MORE = MONEY"), except that the operator precedence of $n.$d + $e.$s causes it to be grouped as $n.($d+$e).$s. A join('',...) might be more efficient anyway.

    By the way, there's no need for a separate solution process for subtraction. You can just rearrange it into an addition problem.