Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!perl
use strict;
use warnings;
my %digits = map { $_ => 1 } ( 0 .. 9 );
sub choose ($&) {
my ( $set, $sub ) = @_;
map { $sub->($_) } grep { $set->{$_} } keys %$set;
}
sub guard { $_[1]->() if $_[0] }
choose { %digits, map { $_ => 0 } ( 0 ) }, sub { my $s = $_[0];
choose { %digits, map { $_ => 0 } ( $s ) }, sub { my $e = $_[0];
choose { %digits, map { $_ => 0 } ( $s, $e ) }, sub { my $n = $_[0];
choose { %digits, map { $_ => 0 } ( $s, $e, $n ) }, sub { my $d = $_[0];
my $send = $s . $e . $n . $d;
choose { %digits, map { $_ => 0 } ( 0, $s, $e, $n, $d ) }, sub { my $m = $_[0];
choose { %digits, map { $_ => 0 } ( $s, $e, $n, $d, $m ) }, sub { my $o = $_[0];
choose { %digits, map { $_ => 0 } ( $s, $e, $n, $d, $m, $o ) }, sub { my $r = $_[0];
my $more = $m . $o . $r . $e ;
choose { %digits, map { $_ => 0 } ( $s, $e, $n, $d, $m, $o, $r ) }, sub { my $y = $_[0];
my $money = $m . $o . $n . $e . $y;
guard( ($send + $more == $money), sub {
printf("%d + %d = %d\n", $send, $more, $money);
exit;
});
}}}}}}}};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment