aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-088/mark-anderson/raku/ch-2.p637
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;
+}