aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-08-16 22:42:15 +0100
committerGitHub <noreply@github.com>2021-08-16 22:42:15 +0100
commite6229c7a184736452f9e9fee0354a58f8147a2f2 (patch)
treee422fdfd8d6df5afae21bc2d25ec097e8c3eff1a
parent06f998178025a01e4e5d1dff82b233ce49c53b69 (diff)
parentfff0de07974a3aa8b32d78d3251567274027999d (diff)
downloadperlweeklychallenge-club-e6229c7a184736452f9e9fee0354a58f8147a2f2.tar.gz
perlweeklychallenge-club-e6229c7a184736452f9e9fee0354a58f8147a2f2.tar.bz2
perlweeklychallenge-club-e6229c7a184736452f9e9fee0354a58f8147a2f2.zip
Merge pull request #4728 from drbaggy/master
Tidied code
-rw-r--r--challenge-126/james-smith/README.md23
-rw-r--r--challenge-126/james-smith/perl/ch-1.pl2
-rw-r--r--challenge-126/james-smith/perl/ch-2.pl21
3 files changed, 21 insertions, 25 deletions
diff --git a/challenge-126/james-smith/README.md b/challenge-126/james-smith/README.md
index 339d6f4d9d..6f2ffee2f8 100644
--- a/challenge-126/james-smith/README.md
+++ b/challenge-126/james-smith/README.md
@@ -64,25 +64,23 @@ up is 6 orders of magnitude.
# Task 2 - Minesweeper Game
-***You are given a rectangle with points marked with either `x` or `*`. Please consider the `x` as a land mine. Write a script to print a rectangle with numbers and `x` as in the Minesweeper game.***
+***You are given a rectangle with points marked with either `x` or `*`. Consider the `x` as a land mine. Write a script to print a rectangle with numbers and `x` as in the Minesweeper game.***
## Solution
```perl
sub solve {
- my @res = ('');
- ## Map input of strings into an array of 1s (bombs) + 0s (spaces)
- my @in = map { [ map { $_ eq 'x' ? 1:0 } split //, $_ ] } @_;
+ my @res = ();
+ my @g = map { [ map { $_ eq 'x' ? 1 : 0 } split //, $_ ] } @_;
- ## Get index of last row or column...
my( $h, $w ) = ( $#_, -1 + length $_[0] );
-
foreach my $y ( 0 .. $h ) {
- $res[-1] .= $in[$y][$_] ? 'x' :
- ( $y ? ( $_ ? $in[$y-1][$_-1] : 0 ) + $in[$y-1][$_] + ( $_ < $w ? $in[$y-1][$_+1] : 0 ) : 0 ) +
- ( $_ ? $in[$y ][$_-1] : 0 ) + $in[$y ][$_] + ( $_ < $w ? $in[$y ][$_+1] : 0 ) +
- ( $y < $h ? ( $_ ? $in[$y+1][$_-1] : 0 ) + $in[$y+1][$_] + ( $_ < $w ? $in[$y+1][$_+1] : 0 ) : 0 ) foreach 0 .. $w;
- push @res, '';
+ push @res, join '', map {
+ $g[$y][$_] ? 'x' :
+ ( $y ? ( $_ ? $g[$y-1][$_-1] : 0 ) + $g[$y-1][$_] + ( $_<$w ? $g[$y-1][$_+1] : 0 ) : 0 ) +
+ ( $_ ? $g[$y ][$_-1] : 0 ) + $g[$y ][$_] + ( $_<$w ? $g[$y ][$_+1] : 0 ) +
+ ( $y<$h ? ( $_ ? $g[$y+1][$_-1] : 0 ) + $g[$y+1][$_] + ( $_<$w ? $g[$y+1][$_+1] : 0 ) : 0 )
+ } 0 .. $w;
}
return join "\n", @res;
}
@@ -91,5 +89,6 @@ sub solve {
There are two stages to this:
1. convert the strings into an array of `1`s and `0`s representing mines and spaces.
- 2. we compute the convulation - counting the number of mines in adjacent squares (or tagging with a "x" if the square is bomb)
+ 2. we compute the convulation - counting the number of mines in adjacent squares (or tagging with a "x" if the square is bomb).
+ Rather than using loops and if statements we use nested ternary operators in a single line
diff --git a/challenge-126/james-smith/perl/ch-1.pl b/challenge-126/james-smith/perl/ch-1.pl
index b3a1866f84..a42ad642c7 100644
--- a/challenge-126/james-smith/perl/ch-1.pl
+++ b/challenge-126/james-smith/perl/ch-1.pl
@@ -62,7 +62,7 @@ sub get_no_one_count_9 {
while($n) {
my $t = $n % 10; ## get last digit
$count = 0 if $t==1; ## Throw everything away we've found a 1
- $count += $t ? ( $t == 1 ? ($pow_9-1) : ($t-1)*$pow_9 ) : 0;
+ $count += !$t ? 0 : $t == 1 ? ($pow_9-1) : ($t-1)*$pow_9;
## 0 it contributes nothing
## 1 contributes 9^X-1
## 2-9 contributes (n-1)9^X
diff --git a/challenge-126/james-smith/perl/ch-2.pl b/challenge-126/james-smith/perl/ch-2.pl
index 0b0900662c..195a1fbc6a 100644
--- a/challenge-126/james-smith/perl/ch-2.pl
+++ b/challenge-126/james-smith/perl/ch-2.pl
@@ -11,24 +11,21 @@ use Data::Dumper qw(Dumper);
my @grid = qw(x***x*xxxx *********x ****x*x*x* ***xx***** x***x****x);
say join "\n",'',@grid,'';
-print solve(@grid);
+say solve(@grid);
say '';
sub solve {
- my @res = ('');
- ## Map input of strings into an array of 1s (bombs) + 0s (spaces)
- my @in = map { [ map { $_ eq 'x' ? 1:0 } split //, $_ ] } @_;
+ my @res = ();
+ my @g = map { [ map { $_ eq 'x' ? 1 : 0 } split //, $_ ] } @_;
- ## Get index of last row or column...
my( $h, $w ) = ( $#_, -1 + length $_[0] );
-
foreach my $y ( 0 .. $h ) {
- $res[-1] .= $in[$y][$_] ? 'x' :
- ( $y ? ( $_ ? $in[$y-1][$_-1] : 0 ) + $in[$y-1][$_] + ( $_ < $w ? $in[$y-1][$_+1] : 0 ) : 0 ) +
- ( $_ ? $in[$y ][$_-1] : 0 ) + $in[$y ][$_] + ( $_ < $w ? $in[$y ][$_+1] : 0 ) +
- ( $y < $h ? ( $_ ? $in[$y+1][$_-1] : 0 ) + $in[$y+1][$_] + ( $_ < $w ? $in[$y+1][$_+1] : 0 ) : 0 ) foreach 0 .. $w;
- push @res, '';
+ push @res, join '', map {
+ $g[$y][$_] ? 'x' :
+ ( $y ? ( $_ ? $g[$y-1][$_-1] : 0 ) + $g[$y-1][$_] + ( $_<$w ? $g[$y-1][$_+1] : 0 ) : 0 ) +
+ ( $_ ? $g[$y ][$_-1] : 0 ) + $g[$y ][$_] + ( $_<$w ? $g[$y ][$_+1] : 0 ) +
+ ( $y<$h ? ( $_ ? $g[$y+1][$_-1] : 0 ) + $g[$y+1][$_] + ( $_<$w ? $g[$y+1][$_+1] : 0 ) : 0 )
+ } 0 .. $w;
}
return join "\n", @res;
}
-