diff options
| author | E. Choroba <choroba@matfyz.cz> | 2024-06-17 10:28:27 +0200 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2024-06-17 10:28:27 +0200 |
| commit | 91ccddc2ba5393c7635a4ede8546ba50423ea8e4 (patch) | |
| tree | bc52c6761d1dbacaf3d91d2924afb025aa3ce055 | |
| parent | 41c7a18ce09761c310487d6af3e2f224cc43a161 (diff) | |
| download | perlweeklychallenge-club-91ccddc2ba5393c7635a4ede8546ba50423ea8e4.tar.gz perlweeklychallenge-club-91ccddc2ba5393c7635a4ede8546ba50423ea8e4.tar.bz2 perlweeklychallenge-club-91ccddc2ba5393c7635a4ede8546ba50423ea8e4.zip | |
Add solutions to 274: Goat Latin & Bus Route by E. Choroba
| -rwxr-xr-x | challenge-274/e-choroba/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-274/e-choroba/perl/ch-2.pl | 53 |
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'; |
