diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-24 02:03:45 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-24 02:03:45 +0100 |
| commit | a7066066d1bd63b5b2e3341d0969adc2ae69cceb (patch) | |
| tree | bec7727c368780847a2c903cbf3b89d5a6cb26d9 | |
| parent | afe0be201b484fd90327bba42037a785f015ffb9 (diff) | |
| parent | a0930ecae073879ad0649d5d2eef43cb22a132ee (diff) | |
| download | perlweeklychallenge-club-a7066066d1bd63b5b2e3341d0969adc2ae69cceb.tar.gz perlweeklychallenge-club-a7066066d1bd63b5b2e3341d0969adc2ae69cceb.tar.bz2 perlweeklychallenge-club-a7066066d1bd63b5b2e3341d0969adc2ae69cceb.zip | |
Merge pull request #10309 from jmaslak/joelle-week-274
Joelle Maslak's solutions
| -rwxr-xr-x | challenge-274/joelle-maslak/perl/1.pl | 18 | ||||
| -rwxr-xr-x | challenge-274/joelle-maslak/perl/2.pl | 54 | ||||
| -rwxr-xr-x | challenge-274/joelle-maslak/raku/1.raku | 15 | ||||
| -rwxr-xr-x | challenge-274/joelle-maslak/raku/2.raku | 57 | ||||
| -rwxr-xr-x | challenge-274/joelle-maslak/test-1.sh | 32 | ||||
| -rwxr-xr-x | challenge-274/joelle-maslak/test-2.sh | 31 |
6 files changed, 207 insertions, 0 deletions
diff --git a/challenge-274/joelle-maslak/perl/1.pl b/challenge-274/joelle-maslak/perl/1.pl new file mode 100755 index 0000000000..b105f02bfc --- /dev/null +++ b/challenge-274/joelle-maslak/perl/1.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl + +use JTM::Boilerplate 'script'; + +MAIN: { + my $sentence = join ' ', @ARGV; + $sentence =~ s/(?<!\S)([aeiou])([^\s]*)/${1}${2}ma/igs; + $sentence =~ s/(?<!\S)([^aeiou])([^\s]*)/${2}${1}ma/igs; + + my (@words) = split /\s+/, $sentence; + my $append = "a"; + for (@words) { + $_ .= $append; + $append .= "a"; + } + $sentence = join " ", @words; + say $sentence; +} diff --git a/challenge-274/joelle-maslak/perl/2.pl b/challenge-274/joelle-maslak/perl/2.pl new file mode 100755 index 0000000000..605bee5f1d --- /dev/null +++ b/challenge-274/joelle-maslak/perl/2.pl @@ -0,0 +1,54 @@ +#!/usr/bin/perl + +use JTM::Boilerplate 'script'; + +MAIN: { + my $input = join '', @ARGV; + $input =~ s/\s+//g; + $input =~ s/^\[\[//; + $input =~ s/\]\]$//; + + my @routes; + my (@routetext) = split /],\[/, $input; + for my $routetext (@routetext) { + my ($interval, $offset, $travel_time) = split /,/, $routetext; + my $route = { + interval => $interval, + offset => $offset, + travel_time => $travel_time, + }; + push @routes, $route; + } + + my @out; + for my $minute (0..59) { + my $best = { + next => undef, + }; + for my $route (@routes) { + my $iteration = 0; + my $bus_at = int(($minute + $route->{interval} - $route->{offset} - 1) / $route->{interval}) * $route->{interval} + $route->{offset}; + my $total_time = $bus_at - $minute + $route->{travel_time}; + + if (!defined($best->{next}) or $best->{next} > $bus_at) { + $best->{next} = $bus_at; + $best->{total_time} = $total_time; + $best->{skip} = 0; # False + next; + } + + if ($total_time < $best->{total_time}) { + $best->{total_time} = $total_time; + if ($best->{next} < $bus_at) { + # We only skip if the two bueses aren't at the same time1 + $best->{skip} = 1; + } + } + } + if ($best->{skip}) { + push @out, $minute; + } + } + + say "[" . join(", ", @out) . "]" +} diff --git a/challenge-274/joelle-maslak/raku/1.raku b/challenge-274/joelle-maslak/raku/1.raku new file mode 100755 index 0000000000..f2cf1c606c --- /dev/null +++ b/challenge-274/joelle-maslak/raku/1.raku @@ -0,0 +1,15 @@ +#!/usr/bin/env raku +use v6.d; + +my $sentence = @*ARGS.join(" "); +$sentence ~~ s:i:g/<!after \S> ( <[aeiou]> \S*)/{$0}ma/; +$sentence ~~ s:i:g/<!after \S> (<-[aeiou]>) (\S*)/{$1}{$0}ma/; + +my (@words) = $sentence.words; +my $append = "a"; +for @words { + $_ ~= $append; + $append ~= "a"; +} +$sentence = @words.join(" "); +say $sentence; diff --git a/challenge-274/joelle-maslak/raku/2.raku b/challenge-274/joelle-maslak/raku/2.raku new file mode 100755 index 0000000000..615923ffdd --- /dev/null +++ b/challenge-274/joelle-maslak/raku/2.raku @@ -0,0 +1,57 @@ +#!/usr/bin/env raku +use v6.d; + +class RouteText { + has $.interval; + has $.offset; + has $.travel_time; +} + +class Best { + has $.total_time is rw; + has $.nextbus is rw; + has $.skip is rw; +} + +my $input = @*ARGS.join(""); +$input ~~ s:g/\s+//; +$input ~~ s/^\[\[//; +$input ~~ s/\]\]$//; + +my @routes; +my (@routetext) = $input.split("],["); +for @routetext -> $routetext { + my ($interval, $offset, $travel_time) = $routetext.split(","); + my $route = RouteText.new(interval => $interval, offset => $offset, travel_time => $travel_time); + @routes.append: $route; +} + +my @out; +for (0..59) -> $minute { + my $best = Best.new(); + for @routes -> $route { + my $iteration = 0; + my $bus_at = Int(($minute + $route.interval - $route.offset - 1) / $route.interval) * $route.interval + $route.offset; + my $total_time = $bus_at - $minute + $route.travel_time; + + if !defined($best.nextbus) or $best.nextbus > $bus_at { + $best.nextbus = $bus_at; + $best.total_time = $total_time; + $best.skip = 0; # False + next; + } + + if $total_time < $best.total_time { + $best.total_time = $total_time; + if $best.nextbus < $bus_at { + # We only skip if the two bueses aren't at the same time1 + $best.skip = 1; + } + } + } + if $best.skip { + @out.append: $minute; + } +} + +say "[" ~ @out.join(", ") ~ "]"; diff --git a/challenge-274/joelle-maslak/test-1.sh b/challenge-274/joelle-maslak/test-1.sh new file mode 100755 index 0000000000..e67f621f3f --- /dev/null +++ b/challenge-274/joelle-maslak/test-1.sh @@ -0,0 +1,32 @@ +#!/bin/bash +set -e + +# +# Copyright (C) 2024 Joelle Maslak +# All Rights Reserved - See License +# + +test_it() { + echo -n $1 $2 ... + OUT=$($1 $2) + if [ "$OUT" == "$3" ] ; then + echo "ok" + else + echo "INCORRECT ($OUT)" + fi +} + +test_combo() { + test_it "$1" "I love perl" "Imaa ovelmaaa erlpmaaaa" + test_it "$1" "Perl and Raku are friends" "erlPmaa andmaaa akuRmaaaa aremaaaaa riendsfmaaaaaa" + test_it "$1" "The Weekly Challenge" "heTmaa eeklyWmaaa hallengeCmaaaa" +} + +do_it() { + test_combo "perl perl/1.pl" + test_combo "raku raku/1.raku" +} + +do_it "$@" + + diff --git a/challenge-274/joelle-maslak/test-2.sh b/challenge-274/joelle-maslak/test-2.sh new file mode 100755 index 0000000000..ed778e82fd --- /dev/null +++ b/challenge-274/joelle-maslak/test-2.sh @@ -0,0 +1,31 @@ +#!/bin/bash +set -e + +# +# Copyright (C) 2024 Joelle Maslak +# All Rights Reserved - See License +# + +test_it() { + echo -n $1 $2 ... + OUT=$($1 $2) + if [ "$OUT" == "$3" ] ; then + echo "ok" + else + echo "INCORRECT ($OUT)" + fi +} + +test_combo() { + test_it "$1" "[ [12, 11, 41], [15, 5, 35] ]" "[36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47]" + test_it "$1" "[ [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]" +} + +do_it() { + test_combo "perl perl/2.pl" + test_combo "raku raku/2.raku" +} + +do_it "$@" + + |
