aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-06-23 16:48:56 +0100
committerGitHub <noreply@github.com>2024-06-23 16:48:56 +0100
commit245bc092caee577b1333483ff276edce42f72e18 (patch)
treed5a00e5aa915619acd2da46ccc5b0dde2b2f4f0c
parent73ccaeb4206cf1b42984dacd0a91d1c5e8f72f6e (diff)
parentb50e6cb7290e2a6f49d77a9f7bfa454c0779f005 (diff)
downloadperlweeklychallenge-club-245bc092caee577b1333483ff276edce42f72e18.tar.gz
perlweeklychallenge-club-245bc092caee577b1333483ff276edce42f72e18.tar.bz2
perlweeklychallenge-club-245bc092caee577b1333483ff276edce42f72e18.zip
Merge pull request #10288 from pjcs00/wk274
Week 274 - Latin buses
-rw-r--r--challenge-274/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-274/peter-campbell-smith/perl/ch-1.pl43
-rwxr-xr-xchallenge-274/peter-campbell-smith/perl/ch-2.pl58
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]);
+ }
+ }
+ }
+}