From bb4198a23850af2c2031d924d2ef39c60ed6ee69 Mon Sep 17 00:00:00 2001 From: juliodcs Date: Tue, 27 Oct 2020 19:37:39 +0100 Subject: juliodcs-week78 --- challenge-084/juliodcs/perl/ch-2-recursive.pl | 39 +++++++++++++++++++++ challenge-084/juliodcs/perl/ch-2.pl | 20 +++++------ challenge-084/juliodcs/raku/ch-2-recursive.raku | 46 +++++++++++++++++++++++++ challenge-084/juliodcs/raku/ch-2.raku | 20 +++++------ 4 files changed, 105 insertions(+), 20 deletions(-) create mode 100644 challenge-084/juliodcs/perl/ch-2-recursive.pl create mode 100644 challenge-084/juliodcs/raku/ch-2-recursive.raku 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); } } } diff --git a/challenge-084/juliodcs/raku/ch-2-recursive.raku b/challenge-084/juliodcs/raku/ch-2-recursive.raku new file mode 100644 index 0000000000..45aabcc457 --- /dev/null +++ b/challenge-084/juliodcs/raku/ch-2-recursive.raku @@ -0,0 +1,46 @@ +#! /usr/bin/raku + +sub count-squares(\matrix where matrix.elems > 1 && matrix.head.elems > 1) { + my \height = matrix.elems; + my \width = matrix.head.elems; + my \max-square-size = min height, width; + + sub all-ones(\y, \x, \size) { + matrix[ y ; x ] + && matrix[ y + size.pred ; x ] + && matrix[ y ; x + size.pred ] + && matrix[ y + size.pred ; x + size.pred ] + } + + multi count-squares(\size where * > max-square-size, \y, \x, \count) { + count + } + + multi count-squares(\size, \y where * > height - size, \x, \count) { + count-squares size.succ, 0, 0, count + } + + multi count-squares(\size, \y, \x where * > width - size, \count) { + count-squares size, y.succ, 0, count + } + + multi count-squares(\size, \y, \x, \count) { + count-squares size, y, x.succ, count + all-ones y, x, size + } + + count-squares 2, 0, 0, 0 +} + +############################################################################# + +use Test; + +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/raku/ch-2.raku b/challenge-084/juliodcs/raku/ch-2.raku index 62752c57a9..10c1fde7c2 100644 --- a/challenge-084/juliodcs/raku/ch-2.raku +++ b/challenge-084/juliodcs/raku/ch-2.raku @@ -1,21 +1,21 @@ #! /usr/bin/raku -sub count-squares(\matrix) { - sub all-ones(\x, \y, \size) { - matrix[ x ; y ] - && matrix[ x + size.pred ; y ] - && matrix[ x ; y + size.pred ] - && matrix[ x + size.pred ; y + size.pred ] +sub count-squares(\matrix where matrix.elems > 1 && matrix.head.elems > 1) { + sub all-ones(\y, \x, \size) { + matrix[ y ; x ] + && matrix[ y + size.pred ; x ] + && matrix[ y ; x + size.pred ] + && matrix[ y + size.pred ; x + size.pred ] } my \h = matrix.elems; my \w = matrix.head.elems; my $count = 0; - for 2 .. max h, w -> \s { - for 0 .. w - s -> \y { - for 0 .. h - s -> \x { - $count++ if all-ones x, y, s; + for 2 .. min h, w -> \s { + for 0 .. h - s -> \y { + for 0 .. w - s -> \x { + $count++ if all-ones y, x, s; } } } -- cgit