[Wellington-pm] SEND + MORE = MONEY

Kirk Jackson kirk at mcs.vuw.ac.nz
Tue Mar 1 18:49:50 PST 2005


> > Grant McLean wrote:
> > 
> >>   SEND
> >>  +MORE
> >>  =====
> >>  MONEY

Hmmm... Procrastination got the better of me, I should be studying today.

I was interested to see how slow a brute-force solution would be (27sec on a
P4 2.6). Not using eval probably helps things (scroll down).

Kirk






















> time ./puzzle.pl
Success:
SEND + MORE = MONEY
7316 + 0823 = 08139
27.222u 0.000s 0:27.26 99.8%    0+0k 0+8io 0pf+0w

#!/usr/bin/perl -Tw

foo([],[0..9]);

sub foo {

    my (@curr) = @{ $_[0]};
    my (@left) = @{ $_[1]};

    if (@left) {
        for (my $i = 0; $i <= $#left; $i++) {
            push @curr, splice(@left, $i, 1, ());
            foo(\@curr, \@left);
            splice(@left, $i, 0, pop @curr);
        }
    } else {
        my ($e, $n, $m, $o, $y, $d, $r, $s) = @curr;

        if (((1000*$s + 100*$e + 10*$n + $d) +
             (1000*$m + 100*$o + 10*$r + $e) -
             (10000*$m + 1000*$o + 100*$n + 10*$e + $y)) == 0) {
            print "Success:\n";
            print "SEND + MORE = MONEY\n";
            print "$s$e$n$d + $m$o$r$e = $m$o$n$e$y\n";
            exit;
        }
    }
}



More information about the Wellington-pm mailing list