aboutsummaryrefslogtreecommitdiff
path: root/challenge-206
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-04 11:37:19 +0000
committerGitHub <noreply@github.com>2023-03-04 11:37:19 +0000
commitcfa31781ca016d7924be4c903793170d1ef80ba9 (patch)
tree7255374c7c2bfe5aae13f73bc0285b0a620897a4 /challenge-206
parentbb58ee26136536db11383634a8959c4a5698b359 (diff)
parent36fcf51fa7395602b2cba0652f68949c92000c85 (diff)
downloadperlweeklychallenge-club-cfa31781ca016d7924be4c903793170d1ef80ba9.tar.gz
perlweeklychallenge-club-cfa31781ca016d7924be4c903793170d1ef80ba9.tar.bz2
perlweeklychallenge-club-cfa31781ca016d7924be4c903793170d1ef80ba9.zip
Merge pull request #7646 from wlmb/challenges
Solve PWC206
Diffstat (limited to 'challenge-206')
-rw-r--r--challenge-206/wlmb/blog.txt2
-rwxr-xr-xchallenge-206/wlmb/perl/ch-1.pl24
-rwxr-xr-xchallenge-206/wlmb/perl/ch-2.pl18
3 files changed, 44 insertions, 0 deletions
diff --git a/challenge-206/wlmb/blog.txt b/challenge-206/wlmb/blog.txt
new file mode 100644
index 0000000000..6fe72cc5db
--- /dev/null
+++ b/challenge-206/wlmb/blog.txt
@@ -0,0 +1,2 @@
+https://wlmb.github.io/2023/02/27/PWC206/
+
diff --git a/challenge-206/wlmb/perl/ch-1.pl b/challenge-206/wlmb/perl/ch-1.pl
new file mode 100755
index 0000000000..ae8b3d96d8
--- /dev/null
+++ b/challenge-206/wlmb/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 206
+# Task 1: Shortest Time
+#
+# See https://wlmb.github.io/2023/02/27/PWC206/#task-1-shortest-time
+use v5.36;
+use List::Util qw(min);
+die <<~"FIN" unless @ARGV;
+ Usage: $0 T1 [T2...]HH:MM_2...
+ to find the shortest time between the time points T1, T2...,
+ each in the 24h format HH:MM
+ FIN
+my ($current, @rest)=sort {$a <=> $b} map {to_minutes($_)} @ARGV;
+push @rest, $current + 24*60; # Add first time as last time, next day
+my $min = min map {my $diff=$_-$current; $current=$_; $diff} @rest;
+say join " ", @ARGV, "->", $min;
+
+sub to_minutes($s){
+ die "Wrong format, expected HH:MM: $s\n" unless $s=~/(\d\d?):(\d\d?)/;
+ my ($hour, $min)=($1, $2);
+ die "Hour should obey 0<=hour<24: $s" unless $hour<24;
+ die "Minute should obey 0<=minute<60: $s" unless $min<60;
+ return $hour*60+$min;
+}
diff --git a/challenge-206/wlmb/perl/ch-2.pl b/challenge-206/wlmb/perl/ch-2.pl
new file mode 100755
index 0000000000..de0a3c9d3a
--- /dev/null
+++ b/challenge-206/wlmb/perl/ch-2.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 206
+# Task 2: Array Pairings
+#
+# See https://wlmb.github.io/2023/02/27/PWC206/#task-2-array-pairings
+use v5.36;
+use List::Util qw(sum);
+die <<~"FIN" unless @ARGV && @ARGV%2==0;
+ Usage: $0 N1 N2 [N3 N4...]
+ to find the maximum of the sum of the minima of each pair (Ni, Nj)
+ taken over all possible pairings. The number of arguments should be even.
+ FIN
+my $i = 0; # counter
+my $max = sum # sum
+ map {$i++%2?():$_} # every second element
+ sort {$a<=>$b} # of the sorted (ascending)
+ @ARGV; # input
+say join " ", @ARGV, "->", $max;