aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-06-19 16:29:32 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-06-19 16:29:32 +0200
commit7f869587539fece3b7892dd2f9f31c26f9af806a (patch)
treedd093fa8e548b018e44f484e78d6b6fc6d1b8bb6
parentffc47a8850ee877978e371d11a01a028862a3f9d (diff)
parent7e2230166c5aa9dc421ca7679397d003274f11a6 (diff)
downloadperlweeklychallenge-club-7f869587539fece3b7892dd2f9f31c26f9af806a.tar.gz
perlweeklychallenge-club-7f869587539fece3b7892dd2f9f31c26f9af806a.tar.bz2
perlweeklychallenge-club-7f869587539fece3b7892dd2f9f31c26f9af806a.zip
Solutions to challenge 274
-rw-r--r--challenge-274/jo-37/blog.txt1
-rwxr-xr-xchallenge-274/jo-37/perl/ch-1.pl62
-rwxr-xr-xchallenge-274/jo-37/perl/ch-2.pl85
3 files changed, 148 insertions, 0 deletions
diff --git a/challenge-274/jo-37/blog.txt b/challenge-274/jo-37/blog.txt
new file mode 100644
index 0000000000..81747a52cc
--- /dev/null
+++ b/challenge-274/jo-37/blog.txt
@@ -0,0 +1 @@
+https://github.sommrey.de/the-bears-den/2024/06/19/ch-274.html
diff --git a/challenge-274/jo-37/perl/ch-1.pl b/challenge-274/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..76d729a45b
--- /dev/null
+++ b/challenge-274/jo-37/perl/ch-1.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [SENTENCE]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+SENTENCE
+ a sentence
+
+EOS
+
+
+### Input and Output
+
+say qq{@{[goat_latin("@ARGV")]}};
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2024/06/19/ch-274.html#task-1
+
+
+sub goat_latin {
+ my $na = 1;
+ no warnings 'uninitialized';
+ shift =~ s#\s*\K(?i:([^aeiou])?)(\w+)#"$2$1m" . ("a" x ++$na)#ger
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is goat_latin("I love Perl"), "Imaa ovelmaaa erlPmaaaa", "example 1";
+ is goat_latin("Perl and Raku are friends"),
+ "erlPmaa andmaaa akuRmaaaa aremaaaaa riendsfmaaaaaa", "example 2";
+ is goat_latin("The Weekly Challenge"), "heTmaa eeklyWmaaa hallengeCmaaaa",
+ "example 3";
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-274/jo-37/perl/ch-2.pl b/challenge-274/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..e1ff23f164
--- /dev/null
+++ b/challenge-274/jo-37/perl/ch-2.pl
@@ -0,0 +1,85 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+use List::Gather;
+use List::Util 'min';
+use experimental 'signatures';
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [I,D,A ...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-verbose
+ enable trace output
+
+I,D,A ...
+ Triplets of service interval, departure minute and arrival minute
+
+EOS
+
+
+### Input and Output
+
+say join ', ', let_leave(map [split ','], @ARGV);
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2024/06/19/ch-274.html#task-2
+
+
+sub s2t ($s) {
+ map {my $d = $s->[0] * $_ + $s->[1]; [$d, $d + $s->[2]]} 0 .. 60 / $s->[0];
+}
+
+sub let_leave {
+ my $seq = 0;
+ my @tt = sort {$a->[0] <=> $b->[0]}
+ map [$_->[0], $seq++],
+ sort {$a->[1] <=> $b->[1]}
+ map s2t($_), @_;
+
+ gather {
+ (\my %expected)->@{0 .. $#tt} = ();
+ my $depart = -1;
+ for my $next (@tt) {
+ my $d = $next->[0];
+ my $seq = $next->[1];
+ take $depart + 1 .. min $d, 59 if $seq > min keys %expected;
+ delete $expected{$seq};
+ $depart = $d;
+ last if $depart > 59;
+ }
+ }
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is [let_leave([12, 11, 41], [15, 5, 35])], [36 .. 47], 'example 1';
+ is [let_leave([12, 3, 41], [15, 9, 35], [30, 5, 25])],
+ [0 .. 3, 25 .. 27, 40 .. 51, 55 .. 59], 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}