diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-02-27 12:17:15 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-03-03 16:10:06 +0100 |
| commit | b936d37736e3dd4f9961c41d571294d02f4fd648 (patch) | |
| tree | 1b9051de62bcfc595ef55d680ce6abf61591e8d1 | |
| parent | 09eef326c170759598ee2d5d35a5aad50be4a11c (diff) | |
| download | perlweeklychallenge-club-b936d37736e3dd4f9961c41d571294d02f4fd648.tar.gz perlweeklychallenge-club-b936d37736e3dd4f9961c41d571294d02f4fd648.tar.bz2 perlweeklychallenge-club-b936d37736e3dd4f9961c41d571294d02f4fd648.zip | |
Solution to task 1
| -rwxr-xr-x | challenge-206/jo-37/perl/ch-1.pl | 63 |
1 files changed, 63 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; +} |
