diff options
| -rw-r--r-- | challenge-187/wlmb/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-187/wlmb/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-187/wlmb/perl/ch-2.pl | 24 |
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: ()"; |
