From c7b3b6dc639b05fec2c84f6959bd69928f9fa909 Mon Sep 17 00:00:00 2001 From: Scimon Date: Tue, 17 Aug 2021 14:22:28 +0100 Subject: Does the basics of the challenge... more soon --- challenge-126/simon-proctor/raku/ch-2.raku | 60 ++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 challenge-126/simon-proctor/raku/ch-2.raku diff --git a/challenge-126/simon-proctor/raku/ch-2.raku b/challenge-126/simon-proctor/raku/ch-2.raku new file mode 100644 index 0000000000..025f530b0d --- /dev/null +++ b/challenge-126/simon-proctor/raku/ch-2.raku @@ -0,0 +1,60 @@ +#!/usr/bin/env raku + +my $example = q:to/END/.chomp; +x * * * x * x x x x +* * * * * * * * * x +* * * * x * x * x * +* * * x x * * * * * +x * * * x * * * * x +END + +multi sub MAIN("test") is hidden-from-USAGE { + my $expected = q:to/END/.chomp; + x 1 0 1 x 2 x x x x + 1 1 0 2 2 4 3 5 5 x + 0 0 1 3 x 3 x 2 x 2 + 1 1 1 x x 4 1 2 2 2 + x 1 1 3 x 2 0 0 1 x + END + + use Test; + + is parse-mines( "x*x" ), [["x","*","x"]]; + is parse-mines( "x * x" ), [["x","*","x"]]; + is find-numbers( "x*x" ), "x 2 x"; + + is find-numbers( $example ), $expected; + + done-testing; +} + +#| Displays the exmaple grid +multi sub MAIN("example") { + $example.say; + ("-" x 19).say; + find-numbers( $example ).say; +} + +sub parse-mines( Str $input ) { + return $input.lines.map( { $_.comb(/\S/).Array } ).Array; +} + +sub find-numbers(Str $s) { + my @grid = parse-mines( $s ); + + my @surround = (1,0,-1) X, (1,0,-1); + + for 0..^@grid.elems -> $y { + for 0..^@grid[$y].elems -> $x { + next if @grid[$y][$x] ~~ 'x'; + my $count = 0; + for @surround -> ($dy,$dx) { + next if 0 ~~ $dy ~~ $dx; + try { $count++ if @grid[$y+$dy][$x+$dx] ~~ 'x'; } + } + @grid[$y][$x] = $count; + } + } + + return @grid.map( { $_.join(" ") } ).join("\n"); +} -- cgit From 3a2671eae0c18f0289911c652d159b78591e5e33 Mon Sep 17 00:00:00 2001 From: Scimon Date: Tue, 17 Aug 2021 14:33:57 +0100 Subject: Now with random maps --- challenge-126/simon-proctor/raku/ch-2.raku | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/challenge-126/simon-proctor/raku/ch-2.raku b/challenge-126/simon-proctor/raku/ch-2.raku index 025f530b0d..55130be0f9 100644 --- a/challenge-126/simon-proctor/raku/ch-2.raku +++ b/challenge-126/simon-proctor/raku/ch-2.raku @@ -35,6 +35,18 @@ multi sub MAIN("example") { find-numbers( $example ).say; } +subset PInt of Int where * > 0; +subset Density of Int where 1 <= * <= 50; + +#| Given a width, height and mine density (1-50%) makes a map then prints the numbers +multi sub MAIN( PInt $width, PInt $height, Density $density ) { + my @opts = ( |("x" xx $density), |("*" xx (100-$density)) ); + my $grid = (^$height).map( { @opts.roll($width).join( " " ) } ).join("\n"); + $grid.say; + ("-" xx $width).join("-").say; + find-numbers( $grid ).say; +} + sub parse-mines( Str $input ) { return $input.lines.map( { $_.comb(/\S/).Array } ).Array; } -- cgit