From a0930ecae073879ad0649d5d2eef43cb22a132ee Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Sun, 23 Jun 2024 19:00:39 -0600 Subject: Joelle Maslak's solutions --- challenge-274/joelle-maslak/perl/1.pl | 18 +++++++++++ challenge-274/joelle-maslak/perl/2.pl | 54 +++++++++++++++++++++++++++++++ challenge-274/joelle-maslak/raku/1.raku | 15 +++++++++ challenge-274/joelle-maslak/raku/2.raku | 57 +++++++++++++++++++++++++++++++++ challenge-274/joelle-maslak/test-1.sh | 32 ++++++++++++++++++ challenge-274/joelle-maslak/test-2.sh | 31 ++++++++++++++++++ 6 files changed, 207 insertions(+) create mode 100755 challenge-274/joelle-maslak/perl/1.pl create mode 100755 challenge-274/joelle-maslak/perl/2.pl create mode 100755 challenge-274/joelle-maslak/raku/1.raku create mode 100755 challenge-274/joelle-maslak/raku/2.raku create mode 100755 challenge-274/joelle-maslak/test-1.sh create mode 100755 challenge-274/joelle-maslak/test-2.sh 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/(? $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/ ( <[aeiou]> \S*)/{$0}ma/; +$sentence ~~ s:i:g/ (<-[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 "$@" + + -- cgit