diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-09-11 10:01:58 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-11 10:01:58 +0100 |
| commit | 7e50c061c7b35383554be4192fe8a969e13aa62c (patch) | |
| tree | 91c6afca07f0d26497f6c91ea482fe3f334b26cf /challenge-077/ash | |
| parent | c3ac5d045f3c9f143f2f55921a5692e6baca1d2d (diff) | |
| parent | 37139a1d46e5ac323ca47abd0eda2f3dc74d61d3 (diff) | |
| download | perlweeklychallenge-club-7e50c061c7b35383554be4192fe8a969e13aa62c.tar.gz perlweeklychallenge-club-7e50c061c7b35383554be4192fe8a969e13aa62c.tar.bz2 perlweeklychallenge-club-7e50c061c7b35383554be4192fe8a969e13aa62c.zip | |
Merge pull request #2251 from ash/master
77-2: Perl solution with regular expressions
Diffstat (limited to 'challenge-077/ash')
| -rw-r--r-- | challenge-077/ash/perl/ch-2.pl | 38 |
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"; +} |
