From abdba5002c8d5038bf6cd217ad7d5d180b3b1ce0 Mon Sep 17 00:00:00 2001 From: mimosinnet Date: Sun, 22 Aug 2021 19:04:35 +0200 Subject: Solutions for challenge 126 --- challenge-126/mimosinnet/raku/2 | 38 +++++++++++++++++ challenge-126/mimosinnet/raku/ch-1.raku | 41 ++++++++++++++++++ challenge-126/mimosinnet/raku/ch-2.raku | 73 +++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 challenge-126/mimosinnet/raku/2 create mode 100644 challenge-126/mimosinnet/raku/ch-1.raku create mode 100644 challenge-126/mimosinnet/raku/ch-2.raku diff --git a/challenge-126/mimosinnet/raku/2 b/challenge-126/mimosinnet/raku/2 new file mode 100644 index 0000000000..075bfd5ad4 --- /dev/null +++ b/challenge-126/mimosinnet/raku/2 @@ -0,0 +1,38 @@ +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-11X/ + +my UInt @numbers_but_one = (1..*).grep( * !~~ /1/ ); +say @numbers_but_one.head(20); + +#| +sub challenge( ) { + +} + +multi sub MAIN( ) { +} + +multi sub MAIN( 'challenge' ) { + my @challenge = ( + ); + + for @challenge -> ($a, $b) { + MAIN($a,$b); + } +} + +multi sub MAIN( 't' ) { +} + +multi sub MAIN( 'test' ) is hidden-from-USAGE { + use Test; + + my @test = ( + ); + + for @test -> ($a, $b, $c ) { + is challenge($a,$b), $c; + } + + done-testing; + +} diff --git a/challenge-126/mimosinnet/raku/ch-1.raku b/challenge-126/mimosinnet/raku/ch-1.raku new file mode 100644 index 0000000000..157d5518bd --- /dev/null +++ b/challenge-126/mimosinnet/raku/ch-1.raku @@ -0,0 +1,41 @@ +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-126/ + +my $tab = " " x 5; + +sub challenge( UInt $n ) { + return (1..$n).grep( * !~~ /1/); +} + +multi sub MAIN( UInt $n ) { + say 'Input: $N = ',$n; + my @result = challenge($n); + say 'Output: ',@result.elems; + say $tab,"There are {@result.elems} numbers between 1 and $n that don't contain digit 1."; + print $tab; + @result.map({ print "$_, "}); + say "\n",$tab,'-' x 70,"\n"; +} + +multi sub MAIN( 'challenge' ) { + my @challenge = ( 15, 25 ); + + for @challenge { + MAIN($_); + } +} + +multi sub MAIN( 'test' ) is hidden-from-USAGE { + use Test; + + my @test = ( + (15, <2 3 4 5 6 7 8 9> ), + (25, <2 3 4 5 6 7 8 9 20 22 23 24 25>) + ); + + for @test -> ($a, $b) { + is challenge($a), $b; + } + + done-testing; + +} diff --git a/challenge-126/mimosinnet/raku/ch-2.raku b/challenge-126/mimosinnet/raku/ch-2.raku new file mode 100644 index 0000000000..fb80267f36 --- /dev/null +++ b/challenge-126/mimosinnet/raku/ch-2.raku @@ -0,0 +1,73 @@ +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-126/ + +# Initial array +my $input = q:to/END/; + x * * * x * x x x x + * * * * * * * * * x + * * * * x * x * x * + * * * x x * * * * * + x * * * x * * * * x +END + +#| +class Mines { + has $!rows; + has $!columns; + has $.input; + has @!solution; + + # this submethod builds all the variables. + submethod TWEAK { + $!rows = self.input.lines.elems; + $!columns = self.input.lines[0].split(/\s*/,:skip-empty).elems; + # Mines uses one dimensional array. + my @data = self.input.trans( 'x' => '1', '*' => '0' ).split(/\s*/,:skip-empty); + my @iterable = @data; + # We need to include the first value of the array: @data[0] = 0. This will be the default + # value when we move outside the defined values of the array + @data.unshift: 0; + + # it finds the solution for each coordinate ($i, $j) + for 1..$!rows -> $i { + for 1..$!columns -> $j { + my $value = @iterable.shift; + # There is a mine in $i, $j if $value = 1; + if $value == 1 { @!solution.push: 'x' } + else { + # We seek for mines around the coordenate ($i, $j) + my @coords; + @coords.push: @data[ coords-to-array( ($i - 1), ($j - 1), $!rows, $!columns ) ]; + @coords.push: @data[ coords-to-array( ($i - 1), ($j ), $!rows, $!columns ) ]; + @coords.push: @data[ coords-to-array( ($i - 1), ($j + 1), $!rows, $!columns ) ]; + @coords.push: @data[ coords-to-array( ($i ), ($j - 1), $!rows, $!columns ) ]; + @coords.push: @data[ coords-to-array( ($i ), ($j + 1), $!rows, $!columns ) ]; + @coords.push: @data[ coords-to-array( ($i + 1), ($j - 1), $!rows, $!columns ) ]; + @coords.push: @data[ coords-to-array( ($i + 1), ($j ), $!rows, $!columns ) ]; + @coords.push: @data[ coords-to-array( ($i + 1), ($j + 1), $!rows, $!columns ) ]; + @!solution.push: @coords.sum; + } + } + } + } + + #| Function to translate coordenates to position in the unidimensional array + sub coords-to-array( $i, $j, $rows, $columns) { + return 0 if ($j < 1 || $i < 1); + return 0 if ($j > $columns || $i > $rows); + return $j + ( $i - 1 ) * $columns; + } + + #| Print the solution + method print-solution { + return @!solution.rotor($!columns).join("\n"); + } + +} + +my $mines = Mines.new( input => $input ); +say 'Input:'; +say $mines.input; +say "\nOutput: "; +say $mines.print-solution; + + -- cgit