aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuis Mochan <mochan@fis.unam.mx>2021-08-21 11:16:07 -0500
committerLuis Mochan <mochan@fis.unam.mx>2021-08-21 11:16:07 -0500
commit116b8036ec0c03f80e1f42f467ef6e6178ece8e1 (patch)
tree868865ccc72fc08869e4c79a599c22d5aaff63d8
parentf9e755c4e4b0cde4d6424fe47191b2b713cc4c47 (diff)
downloadperlweeklychallenge-club-116b8036ec0c03f80e1f42f467ef6e6178ece8e1.tar.gz
perlweeklychallenge-club-116b8036ec0c03f80e1f42f467ef6e6178ece8e1.tar.bz2
perlweeklychallenge-club-116b8036ec0c03f80e1f42f467ef6e6178ece8e1.zip
Simplify code
-rwxr-xr-xchallenge-126/wlmb/perl/ch-2.pl24
1 files changed, 12 insertions, 12 deletions
diff --git a/challenge-126/wlmb/perl/ch-2.pl b/challenge-126/wlmb/perl/ch-2.pl
index b19c1b6b44..5ec2e4e5b0 100755
--- a/challenge-126/wlmb/perl/ch-2.pl
+++ b/challenge-126/wlmb/perl/ch-2.pl
@@ -7,18 +7,17 @@ use warnings;
use strict;
use v5.12;
use PDL;
-use PDL::Image2D;
+use PDL::Image2D; # for conv2d
-my %translate=('x'=>1, '*'=>0);
my $input=process_input();
-my $count=conv2d($input, ones(3,3), {Boundary=>'Truncate'});
-my $output=-10*$input+$count; #mine sites get negative numbers
-# Make strings and remove brackets
-(my $input_str="$input")=~tr(01[])(*x)d;
+my $kernel=pdl([1,1,1],[1,-9,1],[1,1,1]);
+my $output=$input->conv2d($kernel, {Boundary=>'Truncate'});
+# Make strings and format them
+(my $input_str="$input")=~tr(01[])(*x)d; #translate and remove brackets
my $output_str="$output";
-$output_str=~s/-\d+/ x/g;
-$output_str=~s/ +/ /g;
-$output_str=~tr([])()d;
+$output_str=~s/-\d+/ x/g; #identify sites with bombs
+$output_str=~s/ +/ /g; #separate sites with single spaces
+$output_str=~tr([])()d; #remove brackets
say "Input:$input_str\nOutput:$output_str";
sub process_input {
@@ -27,12 +26,13 @@ sub process_input {
while(<>){ # read input from STDIN
chomp;
next if /^\s*$/; #ignore null lines
- die "Only 'x' and '*' and spaces allowed: $_" unless m/^[x*\s]*$/;
- my @line=map {$translate{$_}} split ' ', $_;
+ die "Only 'x' and '*' and spaces allowed: $_" unless m/^[x* \t]*$/;
+ tr(*x \t)(01)d;
+ my @line=split '';
die "All lines should have the same length: $_"
if defined $length and $length!=@line;
$length//=@line;
push @input, [@line];
}
- my $input=pdl [@input];
+ pdl [@input];
}