aboutsummaryrefslogtreecommitdiff
path: root/challenge-126
diff options
context:
space:
mode:
authormimosinnet <mimosinnet@gmail.com>2021-08-22 19:04:35 +0200
committermimosinnet <mimosinnet@gmail.com>2021-08-22 19:04:35 +0200
commitabdba5002c8d5038bf6cd217ad7d5d180b3b1ce0 (patch)
tree395e61a859a8d0c4d990f682d12b6b839db4c4c6 /challenge-126
parented86aba3173945ac27b43d38aca7bd73b7bc02d0 (diff)
downloadperlweeklychallenge-club-abdba5002c8d5038bf6cd217ad7d5d180b3b1ce0.tar.gz
perlweeklychallenge-club-abdba5002c8d5038bf6cd217ad7d5d180b3b1ce0.tar.bz2
perlweeklychallenge-club-abdba5002c8d5038bf6cd217ad7d5d180b3b1ce0.zip
Solutions for challenge 126
Diffstat (limited to 'challenge-126')
-rw-r--r--challenge-126/mimosinnet/raku/238
-rw-r--r--challenge-126/mimosinnet/raku/ch-1.raku41
-rw-r--r--challenge-126/mimosinnet/raku/ch-2.raku73
3 files changed, 152 insertions, 0 deletions
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;
+
+