From 4a7d9c71ac7516dda01631d59845ec03933907be Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 8 Jun 2020 10:45:23 +0100 Subject: Shortest path --- challenge-064/simon-proctor/raku/ch-1.raku | 42 ++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 challenge-064/simon-proctor/raku/ch-1.raku diff --git a/challenge-064/simon-proctor/raku/ch-1.raku b/challenge-064/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..9228404924 --- /dev/null +++ b/challenge-064/simon-proctor/raku/ch-1.raku @@ -0,0 +1,42 @@ +#!/usr/bin/env raku + +use v6.d; + +#| Find the path from the top right of the grid to the +#| Bottom left that sums up to the least amount. +sub MAIN ( + UInt $width, #= Width of the grid + *@cells where { $_.all ~~ UInt && 1 < $_.elems && $_.elems %% $width }, #= Cell values + Bool :v(:$verbose) = False, #= Verbose mode +) { + my @grid = @cells.rotor($width); + say "Width: $width\nGrid:" if $verbose; + say @grid.map( { .join(" ") } ).join("\n") if $verbose; + + my @short-route = route-to( (0,0), (@grid[0].end,@grid.end) ).sort( -> @route { score-route( @grid, @route ) } )[0]; + + @short-route[0].map( -> ($x,$y) { @grid[$y][$x] } ).join(" -> " ).say if $verbose; + + say score-route( @grid, @short-route[0] ); +} + +multi sub score-route( @grid, @route ) { + [+] @route.map( -> ( $x, $y ) { @grid[$y][$x] } ); +} + +multi sub route-to ( @s, @e where { @s ~~ @e } ) { return [ [@s,], ] } + +multi sub route-to( @s, @e ) { + my @routes; + if ( @s[0] !~~ @e[0] ) { + for route-to( ( @s[0]+1, @s[1] ), @e ) -> @route { + @routes.push( @route.unshift( @s ) ); + } + } + if ( @s[1] !~~ @e[1] ) { + for route-to( ( @s[0], @s[1]+1 ), @e ) -> @route { + @routes.push( @route.unshift( @s ) ); + } + } + @routes; +} -- cgit From fc87777844819cf04685204088d9d03974aacad4 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Thu, 11 Jun 2020 13:48:52 +0100 Subject: Challenge 2 --- challenge-064/simon-proctor/raku/ch-2.raku | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 challenge-064/simon-proctor/raku/ch-2.raku diff --git a/challenge-064/simon-proctor/raku/ch-2.raku b/challenge-064/simon-proctor/raku/ch-2.raku new file mode 100644 index 0000000000..435ddf90b0 --- /dev/null +++ b/challenge-064/simon-proctor/raku/ch-2.raku @@ -0,0 +1,13 @@ +#!/usr/bin/env raku + +use v6.d; + +#| Given a string and a list of other strings return those in the main string in order +sub MAIN ( + Str $haystack, #= Target string to search + *@needles #= Strings to search for +) { + my @out = @needles.map( { $_ => $haystack.index($_) } ).grep( { $_.value.defined } ).sort( { $^a.value <=> $^b.value } ).map( *.key ); + (.say for @out ) if @out; + "0".say unless @out; +} -- cgit From a862df922b7f5b3cf1ead52ba59911c618de64ab Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Thu, 11 Jun 2020 14:00:10 +0100 Subject: Simplfy --- challenge-064/simon-proctor/raku/ch-2.raku | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/challenge-064/simon-proctor/raku/ch-2.raku b/challenge-064/simon-proctor/raku/ch-2.raku index 435ddf90b0..56d031c333 100644 --- a/challenge-064/simon-proctor/raku/ch-2.raku +++ b/challenge-064/simon-proctor/raku/ch-2.raku @@ -7,7 +7,6 @@ sub MAIN ( Str $haystack, #= Target string to search *@needles #= Strings to search for ) { - my @out = @needles.map( { $_ => $haystack.index($_) } ).grep( { $_.value.defined } ).sort( { $^a.value <=> $^b.value } ).map( *.key ); - (.say for @out ) if @out; - "0".say unless @out; + my @out = ( @needles.map( { $_ => $haystack.index($_) } ).grep( { $_.value.defined } ).sort( { $^a.value <=> $^b.value } ).map( *.key ) ) || [0]; + .say for @out; } -- cgit From 4b3b5151178dec3c24fee60c242e431dfcebd3c7 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Thu, 11 Jun 2020 14:00:21 +0100 Subject: Simplfy --- challenge-064/simon-proctor/raku/ch-2.raku | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/challenge-064/simon-proctor/raku/ch-2.raku b/challenge-064/simon-proctor/raku/ch-2.raku index 56d031c333..acebf8233e 100644 --- a/challenge-064/simon-proctor/raku/ch-2.raku +++ b/challenge-064/simon-proctor/raku/ch-2.raku @@ -7,6 +7,5 @@ sub MAIN ( Str $haystack, #= Target string to search *@needles #= Strings to search for ) { - my @out = ( @needles.map( { $_ => $haystack.index($_) } ).grep( { $_.value.defined } ).sort( { $^a.value <=> $^b.value } ).map( *.key ) ) || [0]; - .say for @out; + .say for ( @needles.map( { $_ => $haystack.index($_) } ).grep( { $_.value.defined } ).sort( { $^a.value <=> $^b.value } ).map( *.key ) ) || [0]; } -- cgit