aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Shitov <andy@shitov.ru>2020-09-11 10:31:46 +0200
committerAndrew Shitov <andy@shitov.ru>2020-09-11 10:31:46 +0200
commit37139a1d46e5ac323ca47abd0eda2f3dc74d61d3 (patch)
tree33ce0ae6391d4ddb996dec5e0271d85d80df0eda
parentc53084b39a60d87ef29009092d54d151f528dfc0 (diff)
downloadperlweeklychallenge-club-37139a1d46e5ac323ca47abd0eda2f3dc74d61d3.tar.gz
perlweeklychallenge-club-37139a1d46e5ac323ca47abd0eda2f3dc74d61d3.tar.bz2
perlweeklychallenge-club-37139a1d46e5ac323ca47abd0eda2f3dc74d61d3.zip
77-2: Perl solution with regular expressions
-rw-r--r--challenge-077/ash/perl/ch-2.pl38
1 files changed, 38 insertions, 0 deletions
diff --git a/challenge-077/ash/perl/ch-2.pl b/challenge-077/ash/perl/ch-2.pl
new file mode 100644
index 0000000000..789af17490
--- /dev/null
+++ b/challenge-077/ash/perl/ch-2.pl
@@ -0,0 +1,38 @@
+use v5.12;
+
+# Task 2 from
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/
+
+# Comments: https://andrewshitov.com/2020/09/08/lonely-x-the-weekly-challenge-77-task-2/
+
+my @matrix = (
+ [qw( O O O X)],
+ [qw( X O O O)],
+ [qw( O X O X)],
+);
+
+for (@matrix) {
+ unshift @$_, 'O';
+ push @$_, 'O';
+}
+unshift @matrix, [map {'O'} 1..scalar(@{$matrix[0]})];
+push @matrix, [map {'O'} 1..scalar(@{$matrix[0]})];
+
+# use Data::Dumper;
+# say Dumper(\@matrix);
+
+my $matrix = join '', map({join '', @$_} @matrix);
+# say $matrix;
+
+my $width = scalar(@{$matrix[0]});
+my $gap = '.' x ($width - 3);
+my $pattern = "OOO${gap}OXO${gap}OOO";
+
+for my $pos (0 .. length($matrix) - length($pattern)) {
+ # say substr($matrix, $pos) . " ~ " . $pattern;
+ next unless substr($matrix, $pos) =~ /^$pattern/;
+ my $y = int($pos / $width);
+ my $x = $pos - $y * $width;
+
+ say "$y, $x";
+}