diff options
| author | Mark Anderson <mark@frontrangerunner.com> | 2020-11-25 10:01:38 -0700 |
|---|---|---|
| committer | Mark Anderson <mark@frontrangerunner.com> | 2020-11-25 10:01:38 -0700 |
| commit | c0f635f8317b40c304be2b093f22c0d47bcfe019 (patch) | |
| tree | 546fac5d136ccb70c45f46d6d57cdf4466e8e762 | |
| parent | 4553f61ddfe9b5ed152da2479e7e637f4b44d0ed (diff) | |
| download | perlweeklychallenge-club-c0f635f8317b40c304be2b093f22c0d47bcfe019.tar.gz perlweeklychallenge-club-c0f635f8317b40c304be2b093f22c0d47bcfe019.tar.bz2 perlweeklychallenge-club-c0f635f8317b40c304be2b093f22c0d47bcfe019.zip | |
Challenge 88 (Raku)
| -rw-r--r-- | challenge-088/mark-anderson/raku/ch-2.p6 | 37 |
1 files changed, 16 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..34a9b3bc03 100644 --- a/challenge-088/mark-anderson/raku/ch-2.p6 +++ b/challenge-088/mark-anderson/raku/ch-2.p6 @@ -1,3 +1,8 @@ +# +# Abandoning my atrocious solution and going with the +# method used by James Smith, Feng Chang, (and possibly others) +# + use Test; plan 4; @@ -27,28 +32,18 @@ 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]; - } - } +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; +} |
