diff options
| author | E. Choroba <choroba@matfyz.cz> | 2020-01-20 16:42:17 +0100 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2020-01-20 16:42:17 +0100 |
| commit | 9396881350ed0e218205b1112d8ca428e3624f4d (patch) | |
| tree | 9e5a37d2edb99f1e842172dc1629ddafed13fd26 | |
| parent | 008c778923279b75b7eebf43cd5fb392bcba583c (diff) | |
| download | perlweeklychallenge-club-9396881350ed0e218205b1112d8ca428e3624f4d.tar.gz perlweeklychallenge-club-9396881350ed0e218205b1112d8ca428e3624f4d.tar.bz2 perlweeklychallenge-club-9396881350ed0e218205b1112d8ca428e3624f4d.zip | |
Add E. Choroba's solutions to 044 (Only 100, please + Make it $200)
| -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"; |
