aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-03-03 16:14:06 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-03-03 16:14:06 +0100
commit8656b1e35c15e3ca565de5570bf423697bd24ca8 (patch)
treebed53700821d656ec364de67881b759468478937
parent09eef326c170759598ee2d5d35a5aad50be4a11c (diff)
parentdaba8035ff9a31c70a87bc1f09fdb630ff48a387 (diff)
downloadperlweeklychallenge-club-8656b1e35c15e3ca565de5570bf423697bd24ca8.tar.gz
perlweeklychallenge-club-8656b1e35c15e3ca565de5570bf423697bd24ca8.tar.bz2
perlweeklychallenge-club-8656b1e35c15e3ca565de5570bf423697bd24ca8.zip
Solutions to challenge 206
-rwxr-xr-xchallenge-206/jo-37/perl/ch-1.pl63
-rwxr-xr-xchallenge-206/jo-37/perl/ch-2.pl91
2 files changed, 154 insertions, 0 deletions
diff --git a/challenge-206/jo-37/perl/ch-1.pl b/challenge-206/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..923a8f1088
--- /dev/null
+++ b/challenge-206/jo-37/perl/ch-1.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0 '!float';
+use PDL;
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV > 1;
+usage: $0 [-examples] [-tests] [H1:M1 H2:M2...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+H1:M1 H2:M2...
+ list of times in 24h-format HH:MM.
+
+EOS
+
+
+### Input and Output
+
+say shortest_time(@ARGV);
+
+
+### Implementation
+
+sub shortest_time {
+ # Parse times, calculate minutes, convert to "long" piddle and sort.
+ my $times = long(map {/^(\d{2}):(\d{2})$/ ? 60 * $1 + $2 : ()} @_)->qsort;
+ # Take the minimum over the difference of cyclic consecutive time
+ # pairs modulo one day.
+ min +($times->rotate(-1) - $times) % 1440;
+
+
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is shortest_time("00:00", "23:55", "20:00"), 5, 'example 1';
+ is shortest_time("01:01", "00:50", "00:57"), 4, 'example 2';
+ is shortest_time("10:10", "09:30", "09:00", "09:55"), 15, 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is shortest_time(qw(23:00 18:00 01:00)), 120, 'carry minimum over';
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-206/jo-37/perl/ch-2.pl b/challenge-206/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..79c301738a
--- /dev/null
+++ b/challenge-206/jo-37/perl/ch-2.pl
@@ -0,0 +1,91 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0 '!float';
+use PDL;
+use PDL::NiceSlice;
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV && !(@ARGV % 2);
+usage: $0 [-examples] [-tests] [N1 N2...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+N1 N2...
+ even sized list of numbers
+
+EOS
+
+
+### Input and Output
+
+say max_pairsum(@ARGV);
+
+
+### Implementation
+
+# We are given an even sized list of numbers (N₁,...,N₂ₙ). The list
+# shall be partitioned into pairs. There is a target function defined
+# as the sum over the minimum of all the pairs. This target function
+# shall be maximized, i.e. the task is to find a partitioning into pairs
+# where the target is maximal and to provide this maximum value.
+#
+# For the sake of convenience we assume all numbers being distinct. We
+# partition the list into pairs
+# ((P¹₁, P¹₂),...,(Pⁿ₁, Pⁿ₂))
+# Neither the order of pairs, nor the order of the paired numbers are
+# relevant for the target function. Thus we can chose an arbitrary pair
+# P = (a, b), Q = (c, d) and arrange it such that
+# a < b, c < d and a < c
+# by exchanging the pairs and/or the numbers within the pairs without
+# altering the value of the target function. The contributions to the
+# target function are min(P) = a and min(Q) = c.
+#
+# Now suppose b > c, i.e. the two pairs "overlap".
+# We build a different pairing where b and c are
+# exchanged and all other pairs remain unaltered, i.e.
+# P' = (a, c) and Q' = (b, d)
+# The new contributions to the target function are
+# min(P') = a = min(P) (from the precondition a < c) and
+# min(Q') = min(b, d) > min(c, d) = min(Q) (from our assumption b > c
+# and the precondition c < d)
+# All summands in the target function remain unchanged with the
+# exception of Q', which increased from Q.
+#
+# There is only one partitioning into pairs where none of the pairs
+# overlap: Taking the pairs as adjacent elements from the sorted list.
+# From the above considerations, this partitioning maximizes the target
+# function.
+#
+# A long introduction to a short solution:
+sub max_pairsum {
+ # Sum over the first elements of successive pairs taken from the
+ # sorted list.
+ sum pdl(@_)->qsort->(0:-2:2);
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is max_pairsum(1,2,3,4), 4, 'example 1';
+ is max_pairsum(0,2,1,3), 2, 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}