diff options
| author | Util <bruce.gray@acm.org> | 2024-06-22 18:17:46 -0500 |
|---|---|---|
| committer | Util <bruce.gray@acm.org> | 2024-06-22 18:17:46 -0500 |
| commit | 606336b3f80e46f51aa51249dc509c8f6b882654 (patch) | |
| tree | ba82f2d72be24c8db75ff5e484a17c6afe0c96c1 | |
| parent | ffc47a8850ee877978e371d11a01a028862a3f9d (diff) | |
| download | perlweeklychallenge-club-606336b3f80e46f51aa51249dc509c8f6b882654.tar.gz perlweeklychallenge-club-606336b3f80e46f51aa51249dc509c8f6b882654.tar.bz2 perlweeklychallenge-club-606336b3f80e46f51aa51249dc509c8f6b882654.zip | |
Add TWC 274 solutions by Bruce Gray, in Raku only.
| -rw-r--r-- | challenge-274/bruce-gray/raku/ch-1.raku | 25 | ||||
| -rw-r--r-- | challenge-274/bruce-gray/raku/ch-2.raku | 53 |
2 files changed, 78 insertions, 0 deletions
diff --git a/challenge-274/bruce-gray/raku/ch-1.raku b/challenge-274/bruce-gray/raku/ch-1.raku new file mode 100644 index 0000000000..bc3d3c2d1e --- /dev/null +++ b/challenge-274/bruce-gray/raku/ch-1.raku @@ -0,0 +1,25 @@ +sub goat_word ( Str $s --> Str ) { + constant $vowel = <a e i o u>.any; + + my $g = $s.starts-with( $vowel ) + ?? $s + !! $s.substr(1) ~ $s.substr(0,1); # or: `$s.comb.rotate.join;` + + return $g ~ 'ma'; +} +sub task1 ( Str $s --> Str ) { + constant @as = 'a' Xx (1..*); + + return ( $s.words.map(&goat_word) Z~ @as ).join: ' '; +} + + +my @tests = + ( 'I love Perl' , 'Imaa ovelmaaa erlPmaaaa' ), + ( 'Perl and Raku are friends' , 'erlPmaa andmaaa akuRmaaaa aremaaaaa riendsfmaaaaaa' ), + ( 'The Weekly Challenge' , "heTmaa eeklyWmaaa hallengeCmaaaa" ), +; +use Test; plan +@tests; +for @tests -> ( $in_str, $expected ) { + is task1($in_str), $expected; +} diff --git a/challenge-274/bruce-gray/raku/ch-2.raku b/challenge-274/bruce-gray/raku/ch-2.raku new file mode 100644 index 0000000000..c6b926bc09 --- /dev/null +++ b/challenge-274/bruce-gray/raku/ch-2.raku @@ -0,0 +1,53 @@ +# No time this week to find a more clever approach; Packing for https://tprc.us/ ! + +sub task2 ( @timetable --> Seq ) { + for @timetable -> ( $service_interval, $offset, $duration ) { + if 60 !%% $service_interval { + warn "service_interval '$service_interval' does not evenly divide into 60, so results will only be correct for the first hour."; + } + if $service_interval <= $offset { + warn "service_interval '$service_interval' <= offset '$offset'; Unexpected - why dont they back up the offset by one or more service intervals?"; + } + } + + my $bus_number; # For debugging + my @bus_trips; + for @timetable -> ( $service_interval, $offset, $duration ) { + $bus_number++; + my @departs = $offset, *+$service_interval … *≥60; + for @departs -> $depart { + my $arrive = $depart + $duration; + + push @bus_trips, { :$bus_number, :$depart, :$arrive }; + } + } + + @bus_trips .= sort(+*.<depart>); + + my @skip_times; + for @bus_trips.rotor(3 => -2) -> (%a, %b, %c) { + + if [>] (%b, %c)».<arrive> + and [!==] (%b, %c)».<depart> { + + my Range $r = %a.<depart> ^.. %b.<depart>; + + append @skip_times, ($r.list Xmod 60); + } + } + + return @skip_times.sort; +} + +my @tests = + ( ( (12, 11, 41), (15, 5, 35) ) , (36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47) ), + ( ( (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 ) ), + + # From challenge-274/e-choroba/perl/ch-2.pl + ( ( (30, 0, 10), (31, 1, 9) ), [] ), # Arrival at the same time + ( ( (30, 0, 10), (30, 0, 8), (30, 1, 8) ), [] ), # Alternative trumps only one bus +; +use Test; plan +@tests; +for @tests -> ( @in, $expected ) { + is task2(@in), $expected; +} |
