aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuis Mochan <mochan@fis.unam.mx>2025-08-11 20:55:56 -0600
committerLuis Mochan <mochan@fis.unam.mx>2025-08-11 20:55:56 -0600
commitf5b3c18470dea14e57f478bbba2e659b37d86714 (patch)
treee74a9a98fbf3af355dffb895bf3bc7d9d2308603
parent730e172acb159a2fc320a78a6250083328ef1067 (diff)
downloadperlweeklychallenge-club-f5b3c18470dea14e57f478bbba2e659b37d86714.tar.gz
perlweeklychallenge-club-f5b3c18470dea14e57f478bbba2e659b37d86714.tar.bz2
perlweeklychallenge-club-f5b3c18470dea14e57f478bbba2e659b37d86714.zip
Solve PWC334
-rw-r--r--challenge-334/wlmb/blog.txt1
-rwxr-xr-xchallenge-334/wlmb/perl/ch-1.pl26
-rwxr-xr-xchallenge-334/wlmb/perl/ch-2.pl32
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;
+ }
+}