aboutsummaryrefslogtreecommitdiff
path: root/challenge-126
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-08-16 22:26:01 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-08-16 22:26:01 +0100
commitfff0de07974a3aa8b32d78d3251567274027999d (patch)
tree39025675ef3b9998eb921185cb5dcaece2cd0753 /challenge-126
parentcddf830adfb158469a50cc03d0ea77b007d2f23b (diff)
downloadperlweeklychallenge-club-fff0de07974a3aa8b32d78d3251567274027999d.tar.gz
perlweeklychallenge-club-fff0de07974a3aa8b32d78d3251567274027999d.tar.bz2
perlweeklychallenge-club-fff0de07974a3aa8b32d78d3251567274027999d.zip
tidied up code
Diffstat (limited to 'challenge-126')
-rw-r--r--challenge-126/james-smith/perl/ch-1.pl2
-rw-r--r--challenge-126/james-smith/perl/ch-2.pl21
2 files changed, 10 insertions, 13 deletions
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;
}
-