From 9cd5dbe59de0919378066e0bed96d8fdf45b3928 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 23 Nov 2020 09:21:06 +0000 Subject: Challenge 1 --- challenge-088/simon-proctor/raku/ch-1.raku | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 challenge-088/simon-proctor/raku/ch-1.raku diff --git a/challenge-088/simon-proctor/raku/ch-1.raku b/challenge-088/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..0f87839e37 --- /dev/null +++ b/challenge-088/simon-proctor/raku/ch-1.raku @@ -0,0 +1,32 @@ +#!/usr/bin/env raku + +use v6; + +multi sub MAIN( + 'ex1' #= Example 1 (5, 2, 1, 4, 3) +) { + product-array( [5,2,1,4,3] ).join(", ").say; +} + + +multi sub MAIN( + 'ex2' #= Example 2 (2, 1, 4, 3) +) { + product-array( [2,1,4,3] ).join(", ").say; +} + +#| Given a list of Integers return the list of products for each item barring the one at the same index +multi sub MAIN( + *@N where { $_.all ~~ Int } +) { + product-array( @N ).join(", ").say; +} + +sub product-array( @N ) { + my @M; + for @N.keys -> $i { + @M[$i] = [*] @N[ @N.keys.grep(!(*~~$i)) ] + } + @M; +} + -- cgit 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 From 3d2651567f2084816d378b80c89387590748135e Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 23 Nov 2020 11:53:15 +0000 Subject: Remove spurious debugging comment --- challenge-088/simon-proctor/raku/ch-2.raku | 1 - 1 file changed, 1 deletion(-) diff --git a/challenge-088/simon-proctor/raku/ch-2.raku b/challenge-088/simon-proctor/raku/ch-2.raku index 756fdf0044..f35d087fda 100644 --- a/challenge-088/simon-proctor/raku/ch-2.raku +++ b/challenge-088/simon-proctor/raku/ch-2.raku @@ -67,7 +67,6 @@ multi sub spiralize( @grid ) { |@grid[*-1].reverse, |((0^..^@grid.end).reverse.map( { @grid[$_][0] } )), |spiralize( center( @grid ) ) -# @grid } sub center( @grid ) { -- cgit