diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-06-11 14:21:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-11 14:21:50 +0100 |
| commit | 947352068222f1a481a17db5bc367bbf1ad619dd (patch) | |
| tree | 2c1701cd42f80ba1c87b7add602842f70c6520d9 /challenge-064 | |
| parent | 5ebf3f36a21514c2783240417b31cf3d09c4ef3a (diff) | |
| parent | 4b3b5151178dec3c24fee60c242e431dfcebd3c7 (diff) | |
| download | perlweeklychallenge-club-947352068222f1a481a17db5bc367bbf1ad619dd.tar.gz perlweeklychallenge-club-947352068222f1a481a17db5bc367bbf1ad619dd.tar.bz2 perlweeklychallenge-club-947352068222f1a481a17db5bc367bbf1ad619dd.zip | |
Merge pull request #1814 from Scimon/master
I thought I'd put the first one in the other day
Diffstat (limited to 'challenge-064')
| -rw-r--r-- | challenge-064/simon-proctor/raku/ch-1.raku | 42 | ||||
| -rw-r--r-- | challenge-064/simon-proctor/raku/ch-2.raku | 11 |
2 files changed, 53 insertions, 0 deletions
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; +} 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..acebf8233e --- /dev/null +++ b/challenge-064/simon-proctor/raku/ch-2.raku @@ -0,0 +1,11 @@ +#!/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 +) { + .say for ( @needles.map( { $_ => $haystack.index($_) } ).grep( { $_.value.defined } ).sort( { $^a.value <=> $^b.value } ).map( *.key ) ) || [0]; +} |
