aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Proctor <simon.proctor@zoopla.co.uk>2020-11-23 11:52:41 +0000
committerSimon Proctor <simon.proctor@zoopla.co.uk>2020-11-23 11:52:41 +0000
commit89ee325dbd178ebc7219272c8d99c71fbdb9f278 (patch)
tree270481795780e5aaf7581c129b8339eb26b04625
parent9cd5dbe59de0919378066e0bed96d8fdf45b3928 (diff)
downloadperlweeklychallenge-club-89ee325dbd178ebc7219272c8d99c71fbdb9f278.tar.gz
perlweeklychallenge-club-89ee325dbd178ebc7219272c8d99c71fbdb9f278.tar.bz2
perlweeklychallenge-club-89ee325dbd178ebc7219272c8d99c71fbdb9f278.zip
Challenge 2 with lots of options
-rw-r--r--challenge-088/simon-proctor/raku/ch-2.raku75
1 files changed, 75 insertions, 0 deletions
diff --git a/challenge-088/simon-proctor/raku/ch-2.raku b/challenge-088/simon-proctor/raku/ch-2.raku
new file mode 100644
index 0000000000..756fdf0044
--- /dev/null
+++ b/challenge-088/simon-proctor/raku/ch-2.raku
@@ -0,0 +1,75 @@
+#!/usr/bin/env raku
+
+use v6;
+
+my %examples = (
+ 'ex1' => [
+ [ 1, 2, 3 ],
+ [ 4, 5, 6 ],
+ [ 7, 8, 9 ],
+ ],
+ 'ex2' => [
+ [ 1, 2, 3, 4 ],
+ [ 5, 6, 7, 8 ],
+ [ 9, 10, 11, 12 ],
+ [ 13, 14, 15, 16 ],
+ ],
+ 'ex3' => [
+ [ 1, 2, 3 ],
+ ],
+ 'ex4' => [
+ [ 1, 2 ],
+ [ 3, 4 ],
+ ],
+ );
+
+my %*SUB-MAIN-OPTS = :named-anywhere;
+
+#| ex1 or ex2 for the examples
+multi sub MAIN (
+ \k where { %examples{k} } #= ex1 or ex2 for the main examples, ex3, ex4 for simple examples
+) {
+ spiralize( %examples{k} ).join(", ").say;
+}
+
+#| Given a width and a list of values
+#| Print the spiral output
+multi sub MAIN(
+ UInt $w, #= Width of the array
+ *@input where { @input.all ~~ UInt && @input.elems %% $w } #= Grid values
+) {
+ spiralize( @input.rotor( $w ) ).join(", ").say;
+}
+
+#| Given a height and width make a grid from 1 upwards that size then sprial it
+multi sub MAIN(
+ UInt $h, #= Height of grid
+ UInt $w, #= Width of grid
+ Bool :v(:$verbose) = False, #= Draw the grid first
+) {
+ my @grid = (1..($h*$w)).rotor($w);
+ @grid.map( *.map( *.fmt('%3d') ).join(", ") ).join("\n").say if $verbose;
+ spiralize( @grid ).join(", ").say;
+}
+
+
+multi sub spiralize( @grid where { @grid.elems == 1 } ) {
+ @grid[0]
+}
+
+multi sub spiralize( @grid where { @grid.elems == 2 } ) {
+ |@grid[0], |@grid[1].reverse
+}
+
+multi sub spiralize( @grid ) {
+ |@grid[0],
+ |((0^..^@grid.end).map( { @grid[$_][*-1] } )),
+ |@grid[*-1].reverse,
+ |((0^..^@grid.end).reverse.map( { @grid[$_][0] } )),
+ |spiralize( center( @grid ) )
+# @grid
+}
+
+sub center( @grid ) {
+ @grid[0^..^@grid.end].map( -> @a { @a[0^..^@a.end] } );
+}