aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2025-11-17 15:40:15 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2025-11-21 11:53:17 +0100
commit8231cbc63c19add7c222d228a50dcdcecbc4cf42 (patch)
tree50b1c8797e5a99666371eed67c87c5aa359642a5
parent35d74a910321b89e92e3c710e482f1f210339cfb (diff)
downloadperlweeklychallenge-club-8231cbc63c19add7c222d228a50dcdcecbc4cf42.tar.gz
perlweeklychallenge-club-8231cbc63c19add7c222d228a50dcdcecbc4cf42.tar.bz2
perlweeklychallenge-club-8231cbc63c19add7c222d228a50dcdcecbc4cf42.zip
Solution to task 2
-rwxr-xr-xchallenge-348/jo-37/perl/ch-2.pl103
1 files changed, 103 insertions, 0 deletions
diff --git a/challenge-348/jo-37/perl/ch-2.pl b/challenge-348/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..5832d35888
--- /dev/null
+++ b/challenge-348/jo-37/perl/ch-2.pl
@@ -0,0 +1,103 @@
+#!/usr/bin/perl
+
+use v5.26;
+use Test2::V0 -no_srand;
+use Test2::Tools::Subtest 'subtest_streamed';
+use Getopt::Long;
+use experimental 'signatures';
+
+use List::Util 'sum';
+use DateTime::Format::Strptime;
+
+
+### Options and Arguments
+
+my ($tests, $examples, $verbose);
+GetOptions(
+ 'examples!' => \$examples,
+ 'tests!' => \$tests,
+ 'verbose!' => \$verbose,
+) or usage();
+
+run_tests($examples, $tests); # tests do not return
+
+usage() unless @ARGV == 2;
+
+sub usage {
+ die <<~EOS;
+ $0 - convert time
+
+ usage: $0 [-examples] [-tests] ] [SOURCE TARGET]
+
+ -examples
+ run the examples from the challenge
+
+ -tests
+ run some tests
+
+ SOURCE TARGET
+ two time strings in the format HH:MM
+
+ EOS
+}
+
+
+### Input and Output
+
+say convert_time(@ARGV);
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2025/11/21/ch-348.html#task-2
+
+sub convert_time {
+ state $div = [5, 3, 4, 24];
+ state $parser = DateTime::Format::Strptime->new(pattern => "%R",
+ strict => 1, on_error => 'croak');
+
+ my ($s, $t) = map $parser->parse_datetime($_), @_;
+ my $diff = ($t - $s)->in_units('minutes') % (24 * 60);
+
+ use integer;
+ sum map +($diff % $_, $diff /= $_)[0], @$div;
+}
+
+
+### Examples and Tests
+
+sub run_tests ($examples, $tests) {
+ return unless $examples || $tests;
+
+ state sub run_example ($args, $expected, $name, $reason=undef) {
+ my $todo = $reason ? todo $reason : undef;
+ my $result = convert_time(@$args);
+ is $result, $expected,
+ "$name: (@$args) -> $expected";
+ }
+
+ plan 2;
+
+ $examples ? subtest_streamed(examples => sub {
+ my @examples = (
+ [["2:30", "2:45"], 1, 'example 1'],
+ [["11:55", "12:15"], 2, 'example 2'],
+ [["9:00", "13:00"], 4, 'example 3'],
+ [["23:45", "0:30"], 3, 'example 4'],
+ [["14:20", "15:25"], 2, 'example 5'],
+ );
+ plan scalar @examples;
+ run_example @$_ for @examples;
+ }) : pass 'skip examples';
+
+ $tests ? subtest_streamed(tests => sub {
+ my @tests = (
+ [["12:00", "11:59"], 32, 'max'],
+ );
+ plan scalar @tests;
+ run_example @$_ for @tests;
+ }) : pass 'skip tests';
+
+ exit;
+}