diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-12-18 13:09:48 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-18 13:09:48 +0000 |
| commit | 61fa673cce8a11b8cc714350a16d645d2122cbc9 (patch) | |
| tree | f2c7600d9ccc32d8661995cecb0ddf98880e2658 /challenge-248 | |
| parent | efdf320321f734def5899556d23c4b85c3a28ec0 (diff) | |
| parent | e3ad4ebb7065b755ab3e424cd207a58dc7cb23a7 (diff) | |
| download | perlweeklychallenge-club-61fa673cce8a11b8cc714350a16d645d2122cbc9.tar.gz perlweeklychallenge-club-61fa673cce8a11b8cc714350a16d645d2122cbc9.tar.bz2 perlweeklychallenge-club-61fa673cce8a11b8cc714350a16d645d2122cbc9.zip | |
Merge pull request #9257 from andemark/challenge-248
Challenge 248 Solutions (Raku)
Diffstat (limited to 'challenge-248')
| -rw-r--r-- | challenge-248/mark-anderson/raku/ch-1.raku | 46 | ||||
| -rw-r--r-- | challenge-248/mark-anderson/raku/ch-2.raku | 72 |
2 files changed, 118 insertions, 0 deletions
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..9d664435ed --- /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 distances($b) + } + + flat ($head, $middle, $tail)>>.List +} + +sub distances($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..6289177402 --- /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 +} |
