diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-11-25 21:58:26 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-25 21:58:26 +0000 |
| commit | c87d80964bc180e2ee79c2b889d1f66cb1b40ca5 (patch) | |
| tree | 71f1386b729be869d1e0ebf365f8ff1eb9f2d2e0 | |
| parent | 290f5865af83932f6528c4e00aa71ad23cc545ae (diff) | |
| parent | 99d5e965cda6c47e880d6f7ec86737811195b2de (diff) | |
| download | perlweeklychallenge-club-c87d80964bc180e2ee79c2b889d1f66cb1b40ca5.tar.gz perlweeklychallenge-club-c87d80964bc180e2ee79c2b889d1f66cb1b40ca5.tar.bz2 perlweeklychallenge-club-c87d80964bc180e2ee79c2b889d1f66cb1b40ca5.zip | |
Merge pull request #2846 from andemark/branch-for-challenge-088
Challenge 88 (Raku)
| -rw-r--r-- | challenge-088/mark-anderson/raku/ch-2.p6 | 36 |
1 files changed, 15 insertions, 21 deletions
diff --git a/challenge-088/mark-anderson/raku/ch-2.p6 b/challenge-088/mark-anderson/raku/ch-2.p6 index 000716f7a2..23a4269fd5 100644 --- a/challenge-088/mark-anderson/raku/ch-2.p6 +++ b/challenge-088/mark-anderson/raku/ch-2.p6 @@ -27,28 +27,22 @@ cmp-ok spiral(@matrix), &[eqv], [1, 2, 4, 6, 8, 7, 5, 3], "Rows > Cols"; cmp-ok spiral(@matrix), &[eqv], [1, 2, 3, 4, 8, 7, 6, 5], "Cols > Rows"; -sub spiral(@step1) { - my @step2 = ([Z] @step1).reverse; - my @step3 = ([Z] @step2).reverse; - my @step4 = ([Z] @step3).reverse; - my @trips = [Z] @step1, @step2, @step3, @step4; - my $elems = @step1.elems * @step1[0].elems; - my @result; - - for @trips.kv -> $k, @t { - for @t -> @step { - @result.push: @step[$k..*-$k-2]; - } - } +# +# Abandoning my atrocious solution and going with the +# method used by James Smith, Feng Chang, (and possibly others) +# +sub spiral(@matrix) { + my @r; + + while @matrix { + @r.append: |@matrix.shift; + + try {@r.push: .pop} for @matrix; - @result = (@result>>.Array).flat[^$elems]; + try @r.append: $_ given @matrix.pop.reverse; - # handle the case when matrix is 3 X 3, 5 X 5, 7 X 7 - # etc. where I can't seem to get the center element :( - unless @result[*-1] { - my $i = @step1 / 2; - @result[*-1] = @step1[$i][$i]; + try {@r.push: @matrix[$_].shift} for @matrix.end...0; } - @result; -} + @r; +} |
