From d3594f9f292c06c9f9b82e64348382783e29a01d Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 27 Oct 2025 10:03:10 -0600 Subject: Solve PWC345 --- challenge-345/wlmb/blog.txt | 1 + challenge-345/wlmb/perl/ch-1.pl | 27 +++++++++++++++++++++++++++ challenge-345/wlmb/perl/ch-2.pl | 29 +++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 challenge-345/wlmb/blog.txt create mode 100755 challenge-345/wlmb/perl/ch-1.pl create mode 100755 challenge-345/wlmb/perl/ch-2.pl diff --git a/challenge-345/wlmb/blog.txt b/challenge-345/wlmb/blog.txt new file mode 100644 index 0000000000..11ee83e717 --- /dev/null +++ b/challenge-345/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2025/10/27/PWC345/ diff --git a/challenge-345/wlmb/perl/ch-1.pl b/challenge-345/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..7323bfcf84 --- /dev/null +++ b/challenge-345/wlmb/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl +# Perl weekly challenge 345 +# Task 1: Peak Positions +# +# See https://wlmb.github.io/2025/10/27/PWC345/#task-1-peak-positions +use v5.36; +use feature qw(try); +use PDL; +use PDL::NiceSlice; + +die <<~"FIN" unless @ARGV; + Usage: $0 A0 A1... + to find the local maxima of the arrays An, + given by strings of the form + "[X0 X1...]" that may be parsed by PDL. + FIN +for(@ARGV){ + try { + my $ints=pdl($_); + # pad with smaller numbers at boundaries + my $padded=append(append($ints(0)-1, $ints), $ints(-1)-1); + my $indices=which(($padded>$padded->rotate(1))&($padded>$padded->rotate(-1))); + $indices -= 1; # remove left boundaries + say "$_ -> $indices"; + } + catch($e){warn $e;} +} diff --git a/challenge-345/wlmb/perl/ch-2.pl b/challenge-345/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..dbca6230af --- /dev/null +++ b/challenge-345/wlmb/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl +# Perl weekly challenge 345 +# Task 2: Last Visitor +# +# See https://wlmb.github.io/2025/10/27/PWC345/#task-2-last-visitor +use v5.36; +use feature qw(try); +die <<~"FIN" unless @ARGV; + Usage: $0 A0 A1... + where An is a string with space separated numbers, positive + or -1. For each run of -1's output the previously seen positive + numbers, or -1 if they have been exhausted. + FIN +for(@ARGV){ + try { + my $count=0; # count of succesive -1's + my @answer = my @seen =(); + my @input = split " "; + for(@input){ + unshift(@seen, $_), $count=0, next if $_ > 0; + die "Only positive numbers and -1 are allowed: $_" + unless $_ == -1; + push @answer, $count<@seen? $seen[$count]:-1; + ++$count; + } + say "[$_] -> [@answer]"; + } + catch($e){warn $e;} +} -- cgit