diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-11-22 03:34:39 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-22 03:34:39 +0000 |
| commit | 4dbd0939d7c861980e792cdee63df368b503459d (patch) | |
| tree | 99faf84789cf8b59fb92c73c72a03c2107e28fcf | |
| parent | d7545763336302b9aa347a09a12ab852add988b8 (diff) | |
| parent | 508b605030d25ad46a141fe04cd5676ddec18c82 (diff) | |
| download | perlweeklychallenge-club-4dbd0939d7c861980e792cdee63df368b503459d.tar.gz perlweeklychallenge-club-4dbd0939d7c861980e792cdee63df368b503459d.tar.bz2 perlweeklychallenge-club-4dbd0939d7c861980e792cdee63df368b503459d.zip | |
Merge pull request #2808 from simongreen-net/swg-087
sgreen solution to challenge 087
| -rw-r--r-- | challenge-087/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-087/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-087/sgreen/perl/ch-1.pl | 41 | ||||
| -rwxr-xr-x | challenge-087/sgreen/perl/ch-2.pl | 84 |
4 files changed, 128 insertions, 2 deletions
diff --git a/challenge-087/sgreen/README.md b/challenge-087/sgreen/README.md index c76bb4f87a..0281dfc367 100644 --- a/challenge-087/sgreen/README.md +++ b/challenge-087/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 086 +# The Weekly Challenge 087 -Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-086-1k2h) +Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-087-6mjh) diff --git a/challenge-087/sgreen/blog.txt b/challenge-087/sgreen/blog.txt new file mode 100644 index 0000000000..44e25bbd7d --- /dev/null +++ b/challenge-087/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-087-6mj diff --git a/challenge-087/sgreen/perl/ch-1.pl b/challenge-087/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..1ad925190c --- /dev/null +++ b/challenge-087/sgreen/perl/ch-1.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use feature 'say'; + +sub main { + my @N = @_; + my @largest = (); + my @current = (); + + # Check that we have integers + die "You must specify one or more integers\n" unless scalar(@N); + foreach (@N) { + die "The value '$_' does not appear to be an integer\n" + unless /^-?\d+$/; + } + + # Go through the sorted list of numbers + foreach my $n ( sort { $a <=> $b } @N ) { + if ( scalar(@current) and $current[-1] != $n - 1 ) { + # Reset the array if the last number is not $n - 1 + @current = (); + } + + push @current, $n; + if ( scalar(@current) > scalar(@largest) ) { + @largest = @current; + } + } + + if ( scalar(@largest) > 1 ) { + say 'Output: (', join( ', ', @largest ), ')'; + } + else { + say 'Output: 0'; + } +} + +main(@ARGV); diff --git a/challenge-087/sgreen/perl/ch-2.pl b/challenge-087/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..c65c94d299 --- /dev/null +++ b/challenge-087/sgreen/perl/ch-2.pl @@ -0,0 +1,84 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +use List::Util qw(min); + +sub _count_ones { + # Count the number of sequencial ones in the row. There might be a + # better way, but at least this works™ + my $count = 0; + ++$count while $_[$count]; + return $count; +} + +sub main { + my @array = (); + + # Process the input + foreach my $row (@_) { + $row =~ s/[^01]//g; + die "Each row must have at least two '0' or '1'\n" if length($row) < 2; + push @array, [ split //, $row ]; + } + + # Sanity check + die "You must specify at least two rows\n" if scalar(@array) < 2; + foreach my $row ( 1 .. $#array ) { + die "Each row must have the same number of colums\n" + if scalar( @{ $array[0] } ) != scalar( @{ $array[$row] } ); + } + + my $rows = scalar(@array); + my $cols = scalar( @{ $array[0] } ); + + my $max_width = 0; + my $max_height = 0; + + # Go through each rows and column + # The top left of a square cannot be on the right or bottom row (hence -2) + my @squares = (); + foreach my $row ( 0 .. $rows - 2 ) { + foreach my $col ( 0 .. $cols - 2 ) { + my $this_width = + _count_ones( @{ $array[$row] }[ $col .. $cols - 1 ] ); + + # No rectangle if this row has < 2 zeros + next if $this_width < 2; + + # Find the maximum rectangle size + for my $this_height ( 2 .. $rows - $row ) { + $this_width = min( + $this_width, + _count_ones( + @{ $array[ $row + $this_height - 1 ] } + [ $col .. $cols - 1 ] + ) + ); + + # No more rectangles can be made from this starting point + last if $this_width < 2; + + # Update the largest rectangle if this one is bigger + if ( $this_width * $this_height > $max_width * $max_height ) { + $max_width = $this_width; + $max_height = $this_height; + } + } + } + } + + # Display the output + if ($max_width) { + for ( 1 .. $max_height ) { + say '[ ', ( '1 ' x $max_width ), ']'; + } + } + else { + say 'Output: 0'; + } +} + +main(@ARGV); |
