From c0f635f8317b40c304be2b093f22c0d47bcfe019 Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Wed, 25 Nov 2020 10:01:38 -0700 Subject: Challenge 88 (Raku) --- challenge-088/mark-anderson/raku/ch-2.p6 | 37 ++++++++++++++------------------ 1 file 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; +} -- cgit From 2ac7c31f74288a10b7ed04600c786238818cf81a Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Wed, 25 Nov 2020 10:32:56 -0700 Subject: Challenge 88 (Raku) --- challenge-088/mark-anderson/raku/ch-2.p6 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-088/mark-anderson/raku/ch-2.p6 b/challenge-088/mark-anderson/raku/ch-2.p6 index 34a9b3bc03..09abf9c7be 100644 --- a/challenge-088/mark-anderson/raku/ch-2.p6 +++ b/challenge-088/mark-anderson/raku/ch-2.p6 @@ -40,7 +40,7 @@ sub spiral(@matrix) { try {@r.push: .pop} for @matrix; - try @r.append($_) given @matrix.pop.reverse; + try @r.append: $_ given @matrix.pop.reverse; try {@r.push: @matrix[$_].shift} for @matrix.end...0; } -- cgit From 99d5e965cda6c47e880d6f7ec86737811195b2de Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Wed, 25 Nov 2020 10:42:41 -0700 Subject: Challenge 88 (Raku) --- challenge-088/mark-anderson/raku/ch-2.p6 | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/challenge-088/mark-anderson/raku/ch-2.p6 b/challenge-088/mark-anderson/raku/ch-2.p6 index 09abf9c7be..23a4269fd5 100644 --- a/challenge-088/mark-anderson/raku/ch-2.p6 +++ b/challenge-088/mark-anderson/raku/ch-2.p6 @@ -1,8 +1,3 @@ -# -# Abandoning my atrocious solution and going with the -# method used by James Smith, Feng Chang, (and possibly others) -# - use Test; plan 4; @@ -32,6 +27,10 @@ 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"; +# +# Abandoning my atrocious solution and going with the +# method used by James Smith, Feng Chang, (and possibly others) +# sub spiral(@matrix) { my @r; -- cgit