-
-
Save masak/7a1d84435d2bca36b717 to your computer and use it in GitHub Desktop.
SEND + MORE = MONEY, see http://blog.plover.com/prog/haskell/monad-search.html
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
use v5.10; | |
use strict; | |
use warnings; | |
sub choose { | |
my ($choices_ref, $fn) = @_; | |
my @choices = @$choices_ref; | |
for my $value (@choices) { | |
$fn->($value); | |
} | |
} | |
sub guard { | |
my ($condition, $fn) = @_; | |
if ($condition) { | |
$fn->(); | |
} | |
} | |
sub minus { | |
my ($set_ref, @remove) = @_; | |
my @set = @$set_ref; | |
my %remove = map { $_ => 1 } @remove; | |
return [grep { !$remove{$_} } @set]; | |
} | |
sub base_10 { | |
my (@digits) = @_; | |
my $result = 0; | |
while (@digits) { | |
my $digit = shift @digits; | |
$result *= 10; | |
$result += $digit; | |
} | |
return $result; | |
} | |
my @digits = 0..9; | |
choose minus(\@digits, 0), sub { my $s = shift; | |
choose minus(\@digits, $s), sub { my $e = shift; | |
choose minus(\@digits, $s, $e), sub { my $n = shift; | |
choose minus(\@digits, $s, $e, $n), sub { my $d = shift; | |
my $send = base_10($s, $e, $n, $d); | |
choose minus(\@digits, 0, $s, $e, $n, $d), sub { my $m = shift; | |
choose minus(\@digits, $s, $e, $n, $d, $m), sub { my $o = shift; | |
choose minus(\@digits, $s, $e, $n, $d, $m, $o), sub { my $r = shift; | |
my $more = base_10($m, $o, $r, $e); | |
choose minus(\@digits, $s, $e, $n, $d, $m, $o, $r), sub { my $y = shift; | |
my $money = base_10($m, $o, $n, $e, $y); | |
guard $send + $more == $money, sub { | |
say "$send + $more == $money"; | |
}}}}}}}}}; |
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
my @digits = 0..9; | |
my $ITERATIONS = 10000; # this line added for profiling | |
choose @digits ∖ 0, -> $s { | |
choose @digits ∖ $s, -> $e { | |
choose @digits ∖ ($s, $e), -> $n { | |
choose @digits ∖ ($s, $e, $n), -> $d { | |
my $send = :10[$s, $e, $n, $d]; | |
choose @digits ∖ (0, $s, $e, $n, $d), -> $m { | |
choose @digits ∖ ($s, $e, $n, $d, $m), -> $o { | |
choose @digits ∖ ($s, $e, $n, $d, $m, $o), -> $r { | |
my $more = :10[$m, $o, $r, $e]; | |
choose @digits ∖ ($s, $e, $n, $d, $m, $o, $r), -> $y { | |
my $money = :10[$m, $o, $n, $e, $y]; | |
exit unless $ITERATIONS--; # this line added for profiling | |
guard $send + $more == $money, { | |
say "$send + $more == $money"; | |
}}}}}}}}}; | |
sub choose(Set $choices, &fn) { | |
for @$choices -> $value { | |
&fn($value); | |
} | |
} | |
sub guard($condition, &fn) { | |
if $condition { | |
&fn(); | |
} | |
} |
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
my @digits = 0..9; | |
choose @digits ∖ 0, -> $s { | |
choose @digits ∖ $s, -> $e { | |
choose @digits ∖ ($s, $e), -> $n { | |
choose @digits ∖ ($s, $e, $n), -> $d { | |
my $send = :10[$s, $e, $n, $d]; | |
choose @digits ∖ (0, $s, $e, $n, $d), -> $m { | |
choose @digits ∖ ($s, $e, $n, $d, $m), -> $o { | |
choose @digits ∖ ($s, $e, $n, $d, $m, $o), -> $r { | |
my $more = :10[$m, $o, $r, $e]; | |
choose @digits ∖ ($s, $e, $n, $d, $m, $o, $r), -> $y { | |
my $money = :10[$m, $o, $n, $e, $y]; | |
guard $send + $more == $money, { | |
say "$send + $more == $money"; | |
}}}}}}}}}; | |
sub choose(Set $choices, &fn) { | |
for @$choices -> $value { | |
&fn($value); | |
} | |
} | |
sub guard($condition, &fn) { | |
if $condition { | |
&fn(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment