diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-09-05 10:58:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-05 10:58:21 +0100 |
| commit | 81a12b7c560b8ddcc910a1f2cc01cd26542e0c72 (patch) | |
| tree | 9c99839edb082ca4365a759f1072036454e2e18b | |
| parent | 6afd0d631e5abf75309cac73b24031cc7db27ba7 (diff) | |
| parent | b2da59d9203e70dafb40bcbb24d512559bd44e5b (diff) | |
| download | perlweeklychallenge-club-81a12b7c560b8ddcc910a1f2cc01cd26542e0c72.tar.gz perlweeklychallenge-club-81a12b7c560b8ddcc910a1f2cc01cd26542e0c72.tar.bz2 perlweeklychallenge-club-81a12b7c560b8ddcc910a1f2cc01cd26542e0c72.zip | |
Merge pull request #2206 from LubosKolouch/master
Task 2
| -rw-r--r-- | challenge-076/lubos-kolouch/perl/ch-2.pl | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/challenge-076/lubos-kolouch/perl/ch-2.pl b/challenge-076/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..e0cc7f966d --- /dev/null +++ b/challenge-076/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,121 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-2.pl +# +# USAGE: ./ch-2.pl +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-076/ +# +# Task 2 - Word Search +# +# AUTHOR: Lubos Kolouch +#=============================================================================== + +use strict; +use warnings; +use File::Slurp; +use feature qw/say/; + + +sub get_words { + my ($grid, $words, $min_count) = @_; + + + # load the grid into 2D array + my @real_grid; + + my $x_size = scalar @$grid - 1; + my $y_size; + + for my $i (0..scalar @$grid -1) { + my $pos = 0; + for (split / /, $grid->[$i]) { + $real_grid[$i][$pos] = $_; + $pos++; + } + $y_size = $pos; + } + + $y_size--; + + # let's construct a big string of all possible combinations + + my $big_string; + # add all rows + for my $x (0..$x_size) { + for my $y (0..$y_size) { + $big_string .= $real_grid[$x][$y]; + } + # at the end of row we need a break + $big_string .= '_'; + } + + # add all columns + for my $y (0..$y_size) { + for my $x (0..$x_size) { + $big_string .= $real_grid[$x][$y]; + } + $big_string .= '_'; + } + + # add diagonal 1 + for my $x (0..$x_size) { + for my $y (0..$y_size) { + last if $x + $y > $x_size; + $big_string .= $real_grid[$x+$y][$y]; + } + $big_string .= '_'; + } + + # add diagonal 2 + for my $y (0..$y_size) { + + for my $x (0..$x_size) { + last if $x + $y > $y_size; + $big_string .= $real_grid[$x][$y+$x]; + } + $big_string .= '_'; + } + + # add diagonal 3 + for my $x (0..$x_size) { + for my $y (0..$y_size) { + last if $x + $y > $x_size; + $big_string .= $real_grid[$x+$y][$y_size - $y]; + } + $big_string .= '_'; + } + + # add diagonal 4 + for my $y (0..$y_size) { + for my $x (0..$x_size) { + last if $x + $y > $y_size; + $big_string .= $real_grid[$x][$y_size - $y - $x]; + } + $big_string .= '_'; + } + + $big_string .= reverse $big_string; + + + my $count = 0; + + for (@$words) { + next unless length($_) >= $min_count; + $count++ if index($big_string, uc($_)) != -1; + } + + return $count; +} + +my ($grid_file, $words_file) = @ARGV; + +my $grid_ref = read_file($grid_file, array_ref => 1, chomp => 1); +my $words_ref = read_file($words_file, array_ref => 1, chomp => 1); + +use Test::More; + +is (get_words(['AA','BB'],['AA', 'BB', 'CC'],2), 2); + +done_testing; |
