[Wellington-pm] SEND + MORE = MONEY
Grant McLean
grant at mclean.net.nz
Tue Mar 1 13:19:41 PST 2005
On Wed, 2005-03-02 at 09:15 +1300, Finlay Thompson wrote:
> Grant
>
> I did some guesses on paper and got 10 solutions:
...
> However I think you meant that no two of the numbers {S,E,N,D,M,O,R,Y}
> should be the same ?
Umm yes, sorry I did omit to say each digit can only be assigned to one
letter.
I came up with a less complicated solution that runs in a little over
half the time of my original ...
#!/usr/bin/perl -w
use strict;
my $message = 'SEND + MORE = MONEY';
my $letters = join '', sort keys %{{ map { $_, 1} ($message
=~ /([A-Z])/g) }};
my $required = length $letters;
print "$message\n";
solve('', 0..9);
sub solve {
my $digits = shift;
if(length $digits == $required) {
$_ = $message;
eval "tr/$letters/$digits/";
return if(/\b0/);
if(/^(....)...(....)...(.....)/) {
print "$_\n" if($1 + $2 == $3);
}
return;
}
foreach (1.. at _) {
my $next = shift;
solve($digits . $next, @_);
push @_, $next;
}
}
More information about the Wellington-pm
mailing list