From 89ee325dbd178ebc7219272c8d99c71fbdb9f278 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 23 Nov 2020 11:52:41 +0000 Subject: Challenge 2 with lots of options --- challenge-088/simon-proctor/raku/ch-2.raku | 75 ++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 challenge-088/simon-proctor/raku/ch-2.raku 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] } ); +} -- cgit