aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-274/e-choroba/perl/ch-1.pl30
-rwxr-xr-xchallenge-274/e-choroba/perl/ch-2.pl53
2 files changed, 83 insertions, 0 deletions
diff --git a/challenge-274/e-choroba/perl/ch-1.pl b/challenge-274/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..0ea4d63c17
--- /dev/null
+++ b/challenge-274/e-choroba/perl/ch-1.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub goat_latin($sentence) {
+ my $order = 1;
+ $sentence =~ s/(\w+)/goatify($1, ++$order)/ge;
+ return $sentence
+}
+
+sub goatify($word, $order) {
+ if ($word =~ /^[^aeiou]/i) {
+ $word .= substr $word, 0, 1, "";
+ }
+ $word .= 'm' . 'a' x $order;
+ return $word
+}
+
+use Test::More tests => 3;
+
+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';
diff --git a/challenge-274/e-choroba/perl/ch-2.pl b/challenge-274/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..8a0c6b3015
--- /dev/null
+++ b/challenge-274/e-choroba/perl/ch-2.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+use List::Util qw{ min };
+
+use enum qw( INTERVAL OFFSET DURATION );
+
+sub bus_route(@routes) {
+ my %at;
+ for my $route (@routes) {
+ my $minute = $route->[OFFSET];
+ while ($minute <= 120) {
+ undef $at{$minute}{ $minute + $route->[DURATION] };
+ $minute += $route->[INTERVAL];
+ }
+ }
+ my @let_leave;
+ my $previous = 0;
+ for my $minute (0 .. 120) {
+ if (exists $at{$minute}) {
+ my $best = min(keys %{ $at{$minute} });
+ my $i = 1;
+ ++$i while $minute + $i < 120 && ! exists $at{ $minute + $i };
+ if ($minute + $i < 120) {
+ my $best2 = min(keys %{ $at{ $minute + $i } });
+ if ($best2 < $best) {
+ push @let_leave, $previous .. min(59, $minute);
+ }
+ }
+ $previous = $minute + 1;
+ }
+ }
+ return \@let_leave
+}
+
+use Test2::V0;
+plan 2 + 2;
+
+is bus_route([12, 11, 41], [15, 5, 35]),
+ [36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47],
+ 'Example 1';
+
+is bus_route([12, 3, 41], [15, 9, 35], [30, 5, 25]),
+ [0, 1, 2, 3, 25, 26, 27, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
+ 50, 51, 55, 56, 57, 58, 59],
+ 'Example 2';
+
+is bus_route([30, 0, 10], [31, 1, 9]), [], 'Arrival at the same time';
+
+is bus_route([30, 0, 10], [30, 0, 8], [30, 1, 8]), [],
+ 'Alternative trumps only one bus';