diff options
| author | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2024-06-19 12:11:25 +0100 |
|---|---|---|
| committer | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2024-06-19 12:11:25 +0100 |
| commit | b50e6cb7290e2a6f49d77a9f7bfa454c0779f005 (patch) | |
| tree | fe7a0ff01a1658ecae097a0189fa75257c4448a3 | |
| parent | ffc47a8850ee877978e371d11a01a028862a3f9d (diff) | |
| download | perlweeklychallenge-club-b50e6cb7290e2a6f49d77a9f7bfa454c0779f005.tar.gz perlweeklychallenge-club-b50e6cb7290e2a6f49d77a9f7bfa454c0779f005.tar.bz2 perlweeklychallenge-club-b50e6cb7290e2a6f49d77a9f7bfa454c0779f005.zip | |
Week 274 - Latin buses
| -rw-r--r-- | challenge-274/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-274/peter-campbell-smith/perl/ch-1.pl | 43 | ||||
| -rwxr-xr-x | challenge-274/peter-campbell-smith/perl/ch-2.pl | 58 |
3 files changed, 102 insertions, 0 deletions
diff --git a/challenge-274/peter-campbell-smith/blog.txt b/challenge-274/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..0e2e40621e --- /dev/null +++ b/challenge-274/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/274 diff --git a/challenge-274/peter-campbell-smith/perl/ch-1.pl b/challenge-274/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..0f72feb952 --- /dev/null +++ b/challenge-274/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2024-06-17 +use utf8; # Week 274 - task 1 - Goat latin +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +goat_latin("I love Perl"); +goat_latin("Perl and Raku are friends"); +goat_latin("The Weekly Challenge"); +goat_latin(qq[Can't you say 'Hello!' to my mother-in-law?]); + +sub goat_latin { + + my ($sentence, $append, $word, $pre, $post, $goated); + + $sentence = shift; + $append = ''; + + # split sentence into words and punctuation + while ($sentence =~ m|([^\s]+)(\s*)|g) { + $word = $1; + $word =~ m|^([^\w]*)([\w'\-]+)([^\w]*)$|; + ($pre, $word, $post) = ($1, $2, $3); + if ($word =~ m|'$|) { + $word = substr($word, 0, -1); + $post = qq['$post]; + } + + # apply the rules + $word = substr($word, 1) . substr($word, 0, 1) + unless $word =~ m|^[aeiou]|i; + $append .= 'a'; + + # join it all up + $goated .= $pre . $word . 'ma' . $append . $post . ' '; + } + + printf(qq[\nInput: \$sentence = "%s"\n], $sentence); + printf(qq[Output: "%s"\n], substr($goated, 0, -1)); +} diff --git a/challenge-274/peter-campbell-smith/perl/ch-2.pl b/challenge-274/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..9efaa30ea7 --- /dev/null +++ b/challenge-274/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,58 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2024-06-17 +use utf8; # Week 274 - task 2 - Bus route +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +bus_route([12, 11, 41], [15, 5, 35]); +bus_route([12, 3, 41], [15, 9, 35], [30, 5, 25]); +bus_route([20, 0, 13], [15, 14, 15], [30, 15, 7]); + +sub bus_route { + + my ($r, @interval, @offset, @duration, + @leave, @arrive, $take, $time, $best, $soonest, $routes); + + # import and show data + $routes = @_; + say qq[\nInput:\n rt int off dur]; + for $r (0 .. $routes - 1) { + ($interval[$r], $offset[$r], $duration[$r]) = @{$_[$r]}; + printf(" %2d %2d %2d %3d\n", $r + 1, $interval[$r], $offset[$r], $duration[$r]); + } + + say qq[Output:]; + say qq[ at take lv ar not lv ar]; + + # first departure in the hour + for $r (0 .. $routes - 1) { + $leave[$r] = $offset[$r]; + } + + # loop over minutes + for $time (0 .. 59) { + + # get soonest arrival for this $time departure + $soonest = 999; + for $r (0 .. $routes - 1) { + $leave[$r] += $interval[$r] if $leave[$r] < $time; + $arrive[$r] = $leave[$r] + $duration[$r]; + + if ($arrive[$r] < $soonest) { + $soonest = $arrive[$r]; + $best = $r; + } + } + + # see if we can get there sooner by leaving later on another route + for $r (0 .. $routes - 1) { + if ($leave[$r] < $leave[$best] and $arrive[$r] > $arrive[$best]) { + printf(" %02d %d %02d %3d %d %02d %3d\n", + $time, $best + 1, $leave[$best], $arrive[$best], $r + 1, $leave[$r], $arrive[$r]); + } + } + } +} |
