diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-08-12 13:25:05 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-12 13:25:05 +0100 |
| commit | 2fe9107db0d8bb930013f2d6a670ceff19010079 (patch) | |
| tree | 5cbd4f5a8f2c6d5e325a75e6fe8ebc1717e93d76 | |
| parent | 9c8dbdb9f762cb79bf6dcfd15fb407e69706d5e7 (diff) | |
| parent | f5b3c18470dea14e57f478bbba2e659b37d86714 (diff) | |
| download | perlweeklychallenge-club-2fe9107db0d8bb930013f2d6a670ceff19010079.tar.gz perlweeklychallenge-club-2fe9107db0d8bb930013f2d6a670ceff19010079.tar.bz2 perlweeklychallenge-club-2fe9107db0d8bb930013f2d6a670ceff19010079.zip | |
Merge pull request #12507 from wlmb/challenges
Solve PWC334
| -rw-r--r-- | challenge-334/wlmb/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-334/wlmb/perl/ch-1.pl | 26 | ||||
| -rwxr-xr-x | challenge-334/wlmb/perl/ch-2.pl | 32 |
3 files changed, 59 insertions, 0 deletions
diff --git a/challenge-334/wlmb/blog.txt b/challenge-334/wlmb/blog.txt new file mode 100644 index 0000000000..e506cdafa4 --- /dev/null +++ b/challenge-334/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2025/08/11/PWC334/ diff --git a/challenge-334/wlmb/perl/ch-1.pl b/challenge-334/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..be81945a7f --- /dev/null +++ b/challenge-334/wlmb/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl +# Perl weekly challenge 334 +# Task 1: Range Sum +# +# See https://wlmb.github.io/2025/08/11/PWC334/#task-1-range-sum +use v5.36; +use PDL; +use feature qw(try); +die <<~"FIN" unless @ARGV and @ARGV%3==0; + Usage: $0 I1 X1 Y1 I2 X2 Y2... + to sum elements Xi to Yi of the array Ii. + Ii is a string in the format "[i0 i1 i2...]" + where the i's are numbers. + FIN +for my ($s, $x, $y)(@ARGV){ + try{ + my $i=pdl($s); + my $n=$i->nelem; + die "Expected 0<=X<=Y<$n: x=$x, y=$y" + unless 0<=$x<=$y<$n; + say "x=$x, y=$y, ints=$s -> ", $i->slice([$x,$y])->sumover; + } + catch($e){ + say $e; + } +} diff --git a/challenge-334/wlmb/perl/ch-2.pl b/challenge-334/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..498000010f --- /dev/null +++ b/challenge-334/wlmb/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl +# Perl weekly challenge 334 +# Task 2: Nearest Valid Point +# +# See https://wlmb.github.io/2025/08/11/PWC334/#task-2-nearest-valid-point +use v5.36; +use feature qw(try); +use PDL; +die <<~"FIN" unless @ARGV and @ARGV%2==0; + Usage: $0 F1 P1 F2 P2... + to find the index in the array of points Pi of the valid + point nearest to Fi. Fi is input as the string "[x y]" and + Pi as the string "[[x1 y1][x2 y2]...]" + FIN +for my ($first_str, $points_str)(@ARGV){ + try{ + my ($first, $points)=map{pdl $_}($first_str, $points_str); + my $indices=whichND(orover($points==$first)); + my $valid=$points->mv(-1,0)->indexND($indices)->mv(0,-1); + my $manhattan=($valid-$first)->abs->sumover; + my $result=-1; + if($manhattan->nelem){ + my $min=$manhattan->min; + my $close=which($manhattan==$min); + $result=$indices->flat->index($close)->min; + } + say "given=$first, points=$points -> $result"; + } + catch($e){ + say $e; + } +} |
