diff options
| -rwxr-xr-x | challenge-044/e-choroba/perl5/ch-1.pl | 48 | ||||
| -rwxr-xr-x | challenge-044/e-choroba/perl5/ch-2.pl | 17 |
2 files changed, 65 insertions, 0 deletions
diff --git a/challenge-044/e-choroba/perl5/ch-1.pl b/challenge-044/e-choroba/perl5/ch-1.pl new file mode 100755 index 0000000000..a57b89a121 --- /dev/null +++ b/challenge-044/e-choroba/perl5/ch-1.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw{ say }; + +use List::Util qw{ sum }; + +use constant { + NOTHING => 0, + PLUS => 1, + MINUS => 2 +}; + +my @digits = 1 .. 9; + +my %op = ( (NOTHING) => "", + (PLUS) => '+', + (MINUS) => '-'); + +sub apply { + my ($mask) = @_; + return $digits[0] + . join "", + map $op{ $mask->[$_-1] } . $digits[$_], + 1 .. $#digits +} + +sub evaluate { + my ($expression) = @_; + my @terms = $expression =~ /[-+]?[0-9]+/g; + return sum(@terms) +} + +sub increment { + my ($mask) = @_; + my $i = $#$mask; + $mask->[$i--] = NOTHING while $mask->[$i] == MINUS; + ++$mask->[$i]; +} + +my @mask = (NOTHING) x (@digits - 1); +while (grep $_ != MINUS, @mask) { + my $expression = apply(\@mask); + say $expression if 100 == evaluate($expression); + increment(\@mask); +} + +# Verify by piping the output to bc. diff --git a/challenge-044/e-choroba/perl5/ch-2.pl b/challenge-044/e-choroba/perl5/ch-2.pl new file mode 100755 index 0000000000..6e63e62be3 --- /dev/null +++ b/challenge-044/e-choroba/perl5/ch-2.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw{ say }; + +my %possibilities = (1 => []); +while (! exists $possibilities{200}) { + my %next; + for my $p (keys %possibilities) { + $next{ $_ } = [ @{ $possibilities{$p} }, $p ] + for $p + 1, $p * 2; + } + %possibilities = %next; +} + +my @moves = @{ $possibilities{200} }; +say scalar @moves, ": @moves"; |
