Skip to content

Instantly share code, notes, and snippets.

@mrjbq7
Created June 2, 2015 13:41
Show Gist options
  • Save mrjbq7/08c16c46068638cec846 to your computer and use it in GitHub Desktop.
Save mrjbq7/08c16c46068638cec846 to your computer and use it in GitHub Desktop.
#!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