From b936d37736e3dd4f9961c41d571294d02f4fd648 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 27 Feb 2023 12:17:15 +0100 Subject: Solution to task 1 --- challenge-206/jo-37/perl/ch-1.pl | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100755 challenge-206/jo-37/perl/ch-1.pl 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 < 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; +} -- cgit From daba8035ff9a31c70a87bc1f09fdb630ff48a387 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 27 Feb 2023 15:55:15 +0100 Subject: Solution to task 2 --- challenge-206/jo-37/perl/ch-2.pl | 91 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100755 challenge-206/jo-37/perl/ch-2.pl 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 < 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; +} -- cgit