From 37139a1d46e5ac323ca47abd0eda2f3dc74d61d3 Mon Sep 17 00:00:00 2001 From: Andrew Shitov Date: Fri, 11 Sep 2020 10:31:46 +0200 Subject: 77-2: Perl solution with regular expressions --- challenge-077/ash/perl/ch-2.pl | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 challenge-077/ash/perl/ch-2.pl 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"; +} -- cgit