From 80750c8e164c159fe869b652be98a1eef64373dd Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Tue, 19 Dec 2023 21:58:07 -0700 Subject: Joelle Maslak Week 248 Challenge --- challenge-248/joelle-maslak/raku/ch-1.raku | 42 ++++++++++++++++++++++++++++++ challenge-248/joelle-maslak/raku/ch-2.raku | 37 ++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 challenge-248/joelle-maslak/raku/ch-1.raku create mode 100644 challenge-248/joelle-maslak/raku/ch-2.raku diff --git a/challenge-248/joelle-maslak/raku/ch-1.raku b/challenge-248/joelle-maslak/raku/ch-1.raku new file mode 100644 index 0000000000..a9f3bedcfc --- /dev/null +++ b/challenge-248/joelle-maslak/raku/ch-1.raku @@ -0,0 +1,42 @@ +#!/usr/bin/env raku +use v6.d; + +# +# Copyright © 2023 Joelle Maslak +# All Rights Reserved - See License +# + +sub MAIN(Str:D $str = "loveleetcode", Str:D $char = "e") { + if $char.chars ≠ 1 { + say("You must provide exactly one letter as your character input."); + return 1; + } + my @c = $str.split("", :skip-empty); + my @positions; + for @c.kv -> $i, $letter { + @positions.push($i) if $letter eq $char; + } + + if @positions.elems == 0 { + say("The letter '$char' is not found."); + return 2; + } + + my $prev = @c.elems; + my $next = shift @positions; + my @output; + for @c.keys -> $i { + if $next == $i { + @output.push: 0; + $prev = $next; + $next = @positions.elems ?? shift @positions !! @c.elems; + } else { + @output.push: min abs($i - $prev), abs($next - $i); + } + } + + say "(" ~ @output.join(",") ~ ")"; + return 0; +} + + diff --git a/challenge-248/joelle-maslak/raku/ch-2.raku b/challenge-248/joelle-maslak/raku/ch-2.raku new file mode 100644 index 0000000000..5fa392d9d2 --- /dev/null +++ b/challenge-248/joelle-maslak/raku/ch-2.raku @@ -0,0 +1,37 @@ +#!/usr/bin/env raku +use v6.d; + +# +# Copyright © 2023 Joelle Maslak +# All Rights Reserved - See License +# + +sub MAIN(*@rows) { + my @a = gather { + if @rows.elems { + for @rows<> -> $row { + take $row.words.map({Int($^a)}).list; + } + } else { + for $*IN.lines() -> $row { + take $row.words.map({Int($^a)}).list; + } + } + } + + my $row-count = @a.elems(); + my $col-count = @a[0].elems(); + if @a.first( { $^a.elems ≠ $col-count } ) { + die("All rows must have the same number of elements"); + } + + my @b; + for 0..^($row-count - 1) -> $i { + @b.push: []; + for 0..^($col-count - 1) -> $k { + @b[*-1].push: [+] cross(($i, $i+1),($k, $k+1)).map( { @a[$^a[0]; $^a[1]] } ); + } + say join " ", @b[*-1].map: { sprintf("%4d", $^a) }; + } +} + -- cgit