diff options
| -rw-r--r-- | challenge-218/wlmb/blog.txt | 2 | ||||
| -rwxr-xr-x | challenge-218/wlmb/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-218/wlmb/perl/ch-2.pl | 45 |
3 files changed, 77 insertions, 0 deletions
diff --git a/challenge-218/wlmb/blog.txt b/challenge-218/wlmb/blog.txt new file mode 100644 index 0000000000..77169dc91d --- /dev/null +++ b/challenge-218/wlmb/blog.txt @@ -0,0 +1,2 @@ +https://wlmb.github.io/2023/05/22/PWC218/ + diff --git a/challenge-218/wlmb/perl/ch-1.pl b/challenge-218/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..db025ca8f5 --- /dev/null +++ b/challenge-218/wlmb/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl +# Perl weekly challenge 218 +# Task 1: Maximum Product +# +# See https://wlmb.github.io/2023/05/22/PWC218/#task-1-maximum-product +use v5.36; +use List::Util qw(all product); +say <<~"FIN" unless @ARGV >= 3; + Usage: $0 N1 N2 N3 [N4...] + to find the maximum product of three numbers from + the list N1 N2... + FIN +my $no_positive= all {$_<=0} @ARGV; +my $result; +if($no_positive){ + my @sorted=sort {$b <=> $a} @ARGV; + $result=product splice @sorted,0,3; +}else{ + my @sorted = sort {abs($b) <=> abs($a)} @ARGV; + my @result = sort by_strange_criteria splice @sorted, 0, 3; + $result[0] = shift @sorted while((product @result) <= 0 && @sorted); + $result = product @result; +} +say "@ARGV->", $result; +sub by_strange_criteria { + return -1 if $a<0 and $b >=0; + return $b<=>$a if $a < 0 and $b < 0; + return 1 if $b<0 and $a >= 0; + return $a<=>$b; +} diff --git a/challenge-218/wlmb/perl/ch-2.pl b/challenge-218/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..815264ca5a --- /dev/null +++ b/challenge-218/wlmb/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl +# Perl weekly challenge 218 +# Task 2: Matrix Score +# +# See https://wlmb.github.io/2023/05/22/PWC218/#task-2-matrix-score +use v5.36; +use PDL; +use PDL::NiceSlice; +use experimental qw(try); +say <<~"FIN" unless @ARGV; + Usage: $0 M1 M2... + to find the score of matrices M1, M2..., + where each M is of the form [[b_11, b_12...],[b_21, b_22...]...] + and each b_ij is a bit (0 or 1) + FIN +my $p; +for(@ARGV){ + try { + my $x=pdl($_); + die "Matrix should be binary" unless (($x==0)|($x==1))->all; + $p=2**$x->xvals->(-1:0); # descending powers of 2, to convert from bit matrix to decimal + my $y; + $x=$y while(defined ($y=convert($x))); # Make as many conversions as possible + say "$_ -> ", value($x); + } + catch($e){ + say "$e: $_"; + } +} + +sub convert($x){ + for(0,1){ # rows or columns + my $s=$_ ? ":," : ""; # argument to slice second or first dimension + for(0..$x->dim($_)-1){ # for each column or each row + my $y=$x->copy; # make a copy of the argument + $y("$s$_").=!$y("$s$_"); # complement the bits of the row or column + return $y if value($y)>value($x); # return modified matrix if value changed + } + } + return; # undef if no conversion found, done +} + +sub value($z){ # turn binary matrix into number + ($z*$p)->sum +} |
