diff options
| author | juliodcs <julio.dcs@gmail.com> | 2020-10-27 19:37:39 +0100 |
|---|---|---|
| committer | juliodcs <julio.dcs@gmail.com> | 2020-10-27 19:37:39 +0100 |
| commit | bb4198a23850af2c2031d924d2ef39c60ed6ee69 (patch) | |
| tree | b656c3be7952a1ebf9e7399fa03177de80f1422d /challenge-084/juliodcs/perl | |
| parent | cbf73c5afb3183f2b6c35746368aa1372e5c6c88 (diff) | |
| download | perlweeklychallenge-club-bb4198a23850af2c2031d924d2ef39c60ed6ee69.tar.gz perlweeklychallenge-club-bb4198a23850af2c2031d924d2ef39c60ed6ee69.tar.bz2 perlweeklychallenge-club-bb4198a23850af2c2031d924d2ef39c60ed6ee69.zip | |
juliodcs-week78
Diffstat (limited to 'challenge-084/juliodcs/perl')
| -rw-r--r-- | challenge-084/juliodcs/perl/ch-2-recursive.pl | 39 | ||||
| -rw-r--r-- | challenge-084/juliodcs/perl/ch-2.pl | 20 |
2 files changed, 49 insertions, 10 deletions
diff --git a/challenge-084/juliodcs/perl/ch-2-recursive.pl b/challenge-084/juliodcs/perl/ch-2-recursive.pl new file mode 100644 index 0000000000..0f54c32d50 --- /dev/null +++ b/challenge-084/juliodcs/perl/ch-2-recursive.pl @@ -0,0 +1,39 @@ +use strict; +use warnings; +use feature qw(say state); +use experimental 'signatures'; + +sub count_squares($matrix) { + my $h = @{ $matrix }; + my $w = @{ $matrix->[0] }; + my $sq_size = $h < $w ? $h : $w; + + my $_all_ones = sub($y, $x, $size) { + return $matrix->[$y ][$x ] + && $matrix->[$y+$size-1][$x ] + && $matrix->[$y ][$x+$size-1] + && $matrix->[$y+$size-1][$x+$size-1] ? 1 : 0; + }; + + my $_count_squares; + $_count_squares = sub($size, $y, $x, $acc) { + return $size > $sq_size ? $acc + : $y > $h - $size ? $_count_squares->($size + 1, 0, 0, $acc) + : $x > $w - $size ? $_count_squares->($size, $y + 1, 0, $acc) + : $_count_squares->($size, $y, $x + 1, $acc + $_all_ones->($y, $x, $size)); + }; + + return $_count_squares->(2, 0, 0, 0); +} + +use Test::More; + +is count_squares([[1, 1], [1, 1]]), 1, 'Simple matrix'; +is count_squares([[1, 1], [1, 0]]), 0, 'Simple matrix, incomplete'; +is count_squares([[1, 1], [1, 0], [1, 1], [1, 1]]), 1, 'Simple (w != h) matrix'; +is count_squares([[1, 1], [1, 1], [1, 1], [1, 1]]), 3, 'Simple (w != h) matrix, all complete'; +is count_squares([[0, 1, 0, 1], [0, 0, 1, 0], [1, 1, 0, 1], [1, 0, 0, 1]]), 1, 'Example test 1'; +is count_squares([[1, 1, 0, 1], [1, 1, 0, 0], [0, 1, 1, 1], [1, 0, 1, 1]]), 4, 'Example test 2'; +is count_squares([[0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 1]]), 0, 'Example test 3'; + +done_testing; diff --git a/challenge-084/juliodcs/perl/ch-2.pl b/challenge-084/juliodcs/perl/ch-2.pl index 0ae90bd8ed..3eb35f9009 100644 --- a/challenge-084/juliodcs/perl/ch-2.pl +++ b/challenge-084/juliodcs/perl/ch-2.pl @@ -3,11 +3,11 @@ use warnings; use feature 'say'; use experimental 'signatures'; -sub all_ones($matrix, $x, $y, $size) { - return $matrix->[$x ][$y ] - && $matrix->[$x+$size-1][$y ] - && $matrix->[$x ][$y+$size-1] - && $matrix->[$x+$size-1][$y+$size-1]; +sub all_ones($matrix, $y, $x, $size) { + return $matrix->[$y ][$x ] + && $matrix->[$y+$size-1][$x ] + && $matrix->[$y ][$x+$size-1] + && $matrix->[$y+$size-1][$x+$size-1]; } sub count_squares($matrix) { @@ -15,12 +15,12 @@ sub count_squares($matrix) { my $h = @{ $matrix }; my $w = @{ $matrix->[0] }; - my $max = $h > $w ? $h : $w; + my $square_size = $h < $w ? $h : $w; - for my $s (2 .. $max) { - for my $y (0 .. $w - $s) { - for my $x (0 .. $h - $s) { - $count++ if all_ones($matrix, $x, $y, $s); + for my $s (2 .. $square_size) { + for my $y (0 .. $h - $s) { + for my $x (0 .. $w - $s) { + $count++ if all_ones($matrix, $y, $x, $s); } } } |
