diff options
| author | Simon Proctor <simon.proctor@zoopla.co.uk> | 2020-09-07 10:00:40 +0100 |
|---|---|---|
| committer | Simon Proctor <simon.proctor@zoopla.co.uk> | 2020-09-07 10:00:40 +0100 |
| commit | fa2e939347ed4b34605aad080b0ffa517218162e (patch) | |
| tree | 5309b810c2c9df0ac57d201ae1536ee0a2196428 | |
| parent | 63afef8b9bfa507fca0fb5a9610a19043ffa14b9 (diff) | |
| download | perlweeklychallenge-club-fa2e939347ed4b34605aad080b0ffa517218162e.tar.gz perlweeklychallenge-club-fa2e939347ed4b34605aad080b0ffa517218162e.tar.bz2 perlweeklychallenge-club-fa2e939347ed4b34605aad080b0ffa517218162e.zip | |
O and X's
| -rw-r--r-- | challenge-077/simon-proctor/raku/ch-2.raku | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/challenge-077/simon-proctor/raku/ch-2.raku b/challenge-077/simon-proctor/raku/ch-2.raku new file mode 100644 index 0000000000..60c94c7469 --- /dev/null +++ b/challenge-077/simon-proctor/raku/ch-2.raku @@ -0,0 +1,30 @@ +#!/usr/bin/env raku + +use v6; + +my %*SUB-MAIN-OPTS = :named-anywhere; + +#| Given a width and a list of O and X's +#| Makes a grid of the given width and counts the number of X's not touching any others +sub MAIN( + UInt $width, #= Width of the grid + :v(:$verbose) = False, #= Prints the Grid + *@cells where { "O"|"X" ~~ $_.all && $_.elems %% $width && $_.elems >= $width } +) { + my @grid = @cells.rotor($width); + @grid.map( *.join(" ")).join("\n").say if $verbose; + + my $count = 0; + for (0..^$width X, 0..^@grid.elems) -> @vec { + $count++ if @grid[@vec[1]][@vec[0]] ~~ "X" ~~ none(surrounding-cells( @vec, @grid )); + } + say $count; +} + +sub surrounds( @vec ) { + (-1..1 X, -1..1).grep( { ! ( $_[0] == $_[1] == 0 ) } ).map( { [ $_[0]+@vec[0], $_[1]+@vec[1] ] } ).grep( { $_[0]&$_[1] >= 0 } ); +} + +sub surrounding-cells( @vec, @grid ) { + surrounds(@vec).map( -> ($x,$y) { @grid[$y][$x] } ).grep( { defined $_ } ); +} |
