Created
June 2, 2015 13:41
-
-
Save mrjbq7/08c16c46068638cec846 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!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