aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-10-17 17:25:42 +0100
committerGitHub <noreply@github.com>2022-10-17 17:25:42 +0100
commitaa03d3cd478be197cdba5abae53c054d730cf39c (patch)
tree813acb93a7264e49ff1b713e577650f30de2689c
parent877e277d2da6212cbba657d1489deb2ec04b2b1d (diff)
parent953168a1e47e346a5bc5e9afd18fe75788c37532 (diff)
downloadperlweeklychallenge-club-aa03d3cd478be197cdba5abae53c054d730cf39c.tar.gz
perlweeklychallenge-club-aa03d3cd478be197cdba5abae53c054d730cf39c.tar.bz2
perlweeklychallenge-club-aa03d3cd478be197cdba5abae53c054d730cf39c.zip
Merge pull request #6926 from wlmb/master
Solve PWC187
-rw-r--r--challenge-187/wlmb/blog.txt1
-rwxr-xr-xchallenge-187/wlmb/perl/ch-1.pl28
-rwxr-xr-xchallenge-187/wlmb/perl/ch-2.pl24
3 files changed, 53 insertions, 0 deletions
diff --git a/challenge-187/wlmb/blog.txt b/challenge-187/wlmb/blog.txt
new file mode 100644
index 0000000000..42527adad8
--- /dev/null
+++ b/challenge-187/wlmb/blog.txt
@@ -0,0 +1 @@
+https://wlmb.github.io//2022/10/17/PWC187.org
diff --git a/challenge-187/wlmb/perl/ch-1.pl b/challenge-187/wlmb/perl/ch-1.pl
new file mode 100755
index 0000000000..a61a9573b9
--- /dev/null
+++ b/challenge-187/wlmb/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 187
+# Task 1: Days Together
+#
+# See https://wlmb.github.io/2022/10/17/PWC187/#task-1-days-together
+use v5.36;
+use List::Util qw(sum max min);
+sub ddmm2days($x){
+ state @month_lengths=qw(31 28 31 30 31 30 31 31 30 31 30 31);
+ die "Wrong format: $x" unless $x=~/(\d\d)-(\d\d)/;
+ my ($d,$m)=($1, $2);
+ die "Wrong month: $m" unless 1<=$m<=12;
+ die "Wrong day: $d. Only $month_lengths[$m-1] days in month $m"
+ unless 1<=$d<=$month_lengths[$m-1];
+ $d-1+sum @month_lengths[(0..$m-2)]
+}
+die <<"FIN" unless @ARGV==4;
+Usage: $0 fs fe bs be
+to obtain the number of days Foo and Bar are together,
+where fs and fe are the start and end dates of Foo's holidays and
+bs and be are the start and end dates of Bar's holidays. Dates are in the
+format DD-MM.
+FIN
+my ($foo_start, $foo_end, $bar_start, $bar_end)=@ARGV;
+my $days= min(map {ddmm2days($_)} ($foo_end, $bar_end))
+ - max(map {ddmm2days($_)}($foo_start, $bar_start));
+$days=$days<0?0:1+$days;
+say "Overlap between intervals ($foo_start, $foo_end) and ($bar_start, $bar_end) is $days days";
diff --git a/challenge-187/wlmb/perl/ch-2.pl b/challenge-187/wlmb/perl/ch-2.pl
new file mode 100755
index 0000000000..b0c51885d0
--- /dev/null
+++ b/challenge-187/wlmb/perl/ch-2.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 187
+# Task 2: Magical Triplets
+#
+# See https://wlmb.github.io/2022/10/17/PWC187/#task-2-magical-triplets
+use v5.36;
+use Algorithm::Combinatorics qw(combinations);
+use List::Util qw(first);
+die <<FIN unless @ARGV;
+Usage: $0 N1 N2 N3 [N4...]
+to find the maximum ordered triplet with elements from {N1, N2...} that obeys
+the strict triangle inequalities.
+FIN
+my @set = sort {$b<=>$a} @ARGV;
+die "All numbers should be positive" unless $set[-1]>0;
+my $iterator=combinations(\@set, 3);
+while(my $triplet=$iterator->next){
+ die "Expected ordered triplet: $triplet" # Currently uneeded
+ unless $triplet->[0]>=$triplet->[1]>=$triplet->[2];
+ next unless $triplet->[1]+$triplet->[2]>$triplet->[0];
+ say "Input: (", join(", ", @ARGV), ") Output: (", join(", ", @$triplet), ")";
+ exit;
+}
+say "Input: (", join(", ", @ARGV), ") Output: ()";