From ac5e4011d54b977a0251a10a5971b348958b798b Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 18 Dec 2023 09:09:55 +0000 Subject: Challenge 248 Solutions (Raku) --- challenge-248/mark-anderson/raku/ch-1.raku | 46 +++++++++++++++++++ challenge-248/mark-anderson/raku/ch-2.raku | 72 ++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 challenge-248/mark-anderson/raku/ch-1.raku create mode 100644 challenge-248/mark-anderson/raku/ch-2.raku (limited to 'challenge-248') diff --git a/challenge-248/mark-anderson/raku/ch-1.raku b/challenge-248/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..9f9cfc21e3 --- /dev/null +++ b/challenge-248/mark-anderson/raku/ch-1.raku @@ -0,0 +1,46 @@ +#!/usr/bin/env raku +use Test; + +is-deeply shortest-distance("loveleetcode", "e"), (3,2,1,0,1,0,0,1,2,2,1,0); +is-deeply shortest-distance("aaab", "b"), (3,2,1,0); +is-deeply shortest-distance("eabcde", "e"), (0,1,2,2,1,0); +is-deeply shortest-distance("eabcdf", "e"), (0,1,2,3,4,5); +is-deeply shortest-distance("abecd", "e"), (2,1,0,1,2); +is-deeply shortest-distance("abcefg", "e"), (3,2,1,0,1,2); +is-deeply shortest-distance("eeeabecefeeg", "e"), (0,0,0,1,1,0,1,0,1,0,0,1); + +sub shortest-distance($str, $char) +{ + my @a = $str.split(/<$char>+/, :v); + + my $head = @a.shift; + $_ = .chars ?? (.chars...1) !! Empty given $head; + + my $tail = @a.pop; + $_ = .chars ?? (1....chars) !! Empty given $tail; + + @a.push: ~Empty; + + my $middle = gather for @a -> $a, $b + { + take 0 xx $a.chars; + take distance($b) + } + + flat ($head, $middle, $tail)>>.List +} + +sub distance($s) +{ + return Empty unless $s; + my $mid = ($s.chars / 2).ceiling; + my @r = (1...^$mid), ($mid, $mid), ($mid^...1); + @r[1] .= squish unless $s.chars %% 2; + @r.List +} + +# sub shortest-distance-check($str, $char) +# { +# my @zeros = $str.indices($char); +# (^$str.chars).map(-> $k { min @zeros.map({ abs($_ - $k) }) }) +# } diff --git a/challenge-248/mark-anderson/raku/ch-2.raku b/challenge-248/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..60784f7d98 --- /dev/null +++ b/challenge-248/mark-anderson/raku/ch-2.raku @@ -0,0 +1,72 @@ +#!/usr/bin/env raku +use Test; + +is-deeply submatrix-sum([ + [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12] + ]), + + [ + [14, 18, 22], + [30, 34, 38], + ]; + +is-deeply submatrix-sum([ + [1, 0, 0, 0], + [0, 1, 0, 0], + [0, 0, 1, 0], + [0, 0, 0, 1] + ]), + + [ + [2, 1, 0], + [1, 2, 1], + [0, 1, 2] + ]; + +is-deeply submatrix-sum([ + [ 22, 4, 20, 24 ], + [ 21, 16, 23, 18 ], + [ 3, 12, 19, 5 ], + [ 15, 7, 10, 2 ], + [ 17, 11, 6, 13 ], + [ 9, 8, 14, 1 ] + ]), + + [ + [ 63, 63, 85 ], + [ 52, 70, 65 ], + [ 37, 48, 36 ], + [ 50, 34, 31 ], + [ 45, 39, 34 ] + ]; + +is-deeply submatrix-sum([ + [ 26, 50, 10, 54, 42, 31, 45, 58 ], + [ 29, 64, 23, 35, 15, 41, 27, 17 ], + [ 55, 37, 1, 30, 52, 53, 21, 46 ], + [ 3, 2, 6, 36, 11, 13, 19, 22 ], + [ 61, 47, 32, 39, 49, 62, 9, 12 ], + [ 20, 60, 44, 7, 59, 34, 14, 16 ], + [ 8, 24, 38, 43, 56, 48, 57, 25 ], + [ 5, 40, 33, 28, 63, 18, 4, 51 ] + ]), + + [ + [ 169, 147, 122, 146, 129, 144, 147 ], + [ 185, 125, 89, 132, 161, 142, 111 ], + [ 97, 46, 73, 129, 129, 106, 108 ], + [ 113, 87, 113, 135, 135, 103, 62 ], + [ 188, 183, 122, 154, 204, 119, 51 ], + [ 112, 166, 132, 165, 197, 153, 112 ], + [ 77, 135, 142, 190, 185, 127, 137 ] + ]; + +sub submatrix-sum(@a) +{ + @a.map({ .rotor(2 => -1)>>.Array }) + .rotor(2 => -1) + .map({ (zip $_).map({ .flat.sum }).Array }) + .Array +} -- cgit From 5f62b3ed1b23c5455f09ad1a09f087daa3e55f3e Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 18 Dec 2023 09:39:42 +0000 Subject: Challenge 248 Solutions (Raku) --- challenge-248/mark-anderson/raku/ch-2.raku | 52 +++++++++++++++--------------- 1 file changed, 26 insertions(+), 26 deletions(-) (limited to 'challenge-248') diff --git a/challenge-248/mark-anderson/raku/ch-2.raku b/challenge-248/mark-anderson/raku/ch-2.raku index 60784f7d98..6289177402 100644 --- a/challenge-248/mark-anderson/raku/ch-2.raku +++ b/challenge-248/mark-anderson/raku/ch-2.raku @@ -26,41 +26,41 @@ is-deeply submatrix-sum([ ]; is-deeply submatrix-sum([ - [ 22, 4, 20, 24 ], - [ 21, 16, 23, 18 ], - [ 3, 12, 19, 5 ], - [ 15, 7, 10, 2 ], - [ 17, 11, 6, 13 ], - [ 9, 8, 14, 1 ] + [22, 4, 20, 24], + [21, 16, 23, 18], + [ 3, 12, 19, 5], + [15, 7, 10, 2], + [17, 11, 6, 13], + [ 9, 8, 14, 1] ]), [ - [ 63, 63, 85 ], - [ 52, 70, 65 ], - [ 37, 48, 36 ], - [ 50, 34, 31 ], - [ 45, 39, 34 ] + [63, 63, 85], + [52, 70, 65], + [37, 48, 36], + [50, 34, 31], + [45, 39, 34] ]; is-deeply submatrix-sum([ - [ 26, 50, 10, 54, 42, 31, 45, 58 ], - [ 29, 64, 23, 35, 15, 41, 27, 17 ], - [ 55, 37, 1, 30, 52, 53, 21, 46 ], - [ 3, 2, 6, 36, 11, 13, 19, 22 ], - [ 61, 47, 32, 39, 49, 62, 9, 12 ], - [ 20, 60, 44, 7, 59, 34, 14, 16 ], - [ 8, 24, 38, 43, 56, 48, 57, 25 ], - [ 5, 40, 33, 28, 63, 18, 4, 51 ] + [26, 50, 10, 54, 42, 31, 45, 58], + [29, 64, 23, 35, 15, 41, 27, 17], + [55, 37, 1, 30, 52, 53, 21, 46], + [ 3, 2, 6, 36, 11, 13, 19, 22], + [61, 47, 32, 39, 49, 62, 9, 12], + [20, 60, 44, 7, 59, 34, 14, 16], + [ 8, 24, 38, 43, 56, 48, 57, 25], + [ 5, 40, 33, 28, 63, 18, 4, 51] ]), [ - [ 169, 147, 122, 146, 129, 144, 147 ], - [ 185, 125, 89, 132, 161, 142, 111 ], - [ 97, 46, 73, 129, 129, 106, 108 ], - [ 113, 87, 113, 135, 135, 103, 62 ], - [ 188, 183, 122, 154, 204, 119, 51 ], - [ 112, 166, 132, 165, 197, 153, 112 ], - [ 77, 135, 142, 190, 185, 127, 137 ] + [169, 147, 122, 146, 129, 144, 147], + [185, 125, 89, 132, 161, 142, 111], + [ 97, 46, 73, 129, 129, 106, 108], + [113, 87, 113, 135, 135, 103, 62], + [188, 183, 122, 154, 204, 119, 51], + [112, 166, 132, 165, 197, 153, 112], + [ 77, 135, 142, 190, 185, 127, 137] ]; sub submatrix-sum(@a) -- cgit From e3ad4ebb7065b755ab3e424cd207a58dc7cb23a7 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 18 Dec 2023 10:22:41 +0000 Subject: Challenge 248 Solutions (Raku) --- challenge-248/mark-anderson/raku/ch-1.raku | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'challenge-248') diff --git a/challenge-248/mark-anderson/raku/ch-1.raku b/challenge-248/mark-anderson/raku/ch-1.raku index 9f9cfc21e3..9d664435ed 100644 --- a/challenge-248/mark-anderson/raku/ch-1.raku +++ b/challenge-248/mark-anderson/raku/ch-1.raku @@ -24,13 +24,13 @@ sub shortest-distance($str, $char) my $middle = gather for @a -> $a, $b { take 0 xx $a.chars; - take distance($b) + take distances($b) } flat ($head, $middle, $tail)>>.List } -sub distance($s) +sub distances($s) { return Empty unless $s; my $mid = ($s.chars / 2).ceiling; -- cgit