Skip to content

Instantly share code, notes, and snippets.

@smls
Created May 26, 2015 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smls/f41a4384e03d58dd18e4 to your computer and use it in GitHub Desktop.
Save smls/f41a4384e03d58dd18e4 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use experimental 'signatures';
use re 'eval';
use List::Util qw(reduce);
sub amb($var, @a) {
join(' || ', map { "(?{ $var = $_ })" } @a);
}
sub remove($s1, @s2) {
my %s2 = map { defined $_ ? ($_ => 1) : () } @s2;
return grep { not defined $s2{$_} } @$s1;
}
sub to_number(@digits) {
reduce { $a*10 + $b } 0, @digits;
}
my @digits = (0..9);
my ($s, $e, $n, $d, $m, $o, $r, $y);
my ($send, $more, $money) = (0,0,0);
'' =~ m/
(??{ amb('$s', remove(\@digits, 0)) })
(??{ amb('$e', remove(\@digits, $s)) })
(??{ amb('$n', remove(\@digits, $s, $e)) })
(??{ amb('$d', remove(\@digits, $s, $e, $n)) })
(?{ $send = to_number($s, $e, $n, $d); })
(??{ amb('$m', remove(\@digits, 0, $s, $e, $n, $d)) })
(??{ amb('$o', remove(\@digits, $s, $e, $n, $d, $m)) })
(??{ amb('$r', remove(\@digits, $s, $e, $n, $d, $m, $o)) })
(?{ $more = to_number($m, $o, $r, $e) })
(??{ amb('$y', remove(\@digits, $s, $e, $n, $d, $m, $o, $r)) })
(?{ $money = to_number($m, $o, $n, $e, $y) })
(?{ say "$send + $more == $money" }) # DEBUG
(?(?{ $send + $more == $money })|(*FAIL))
/x;
say "$send + $more == $money";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment