aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2021-08-30 21:05:21 +1000
committerSimon Green <mail@simon.green>2021-08-30 21:05:21 +1000
commit91109450ccceb873064264b473bdf2df34b25963 (patch)
treee30a2a0b0b07c4439394f4ed91c37e9764d1b9ab
parent903abdef122a8081f238f0b3a9e8c02c5929b2b8 (diff)
downloadperlweeklychallenge-club-91109450ccceb873064264b473bdf2df34b25963.tar.gz
perlweeklychallenge-club-91109450ccceb873064264b473bdf2df34b25963.tar.bz2
perlweeklychallenge-club-91109450ccceb873064264b473bdf2df34b25963.zip
sgreen solution to challenge 128
-rw-r--r--challenge-128/sgreen/README.md4
-rw-r--r--challenge-128/sgreen/blog.txt1
-rwxr-xr-xchallenge-128/sgreen/perl/ch-1.pl71
-rwxr-xr-xchallenge-128/sgreen/perl/ch-2.pl51
-rw-r--r--challenge-128/sgreen/perl/example1.txt3
-rw-r--r--challenge-128/sgreen/perl/example2.txt3
6 files changed, 131 insertions, 2 deletions
diff --git a/challenge-128/sgreen/README.md b/challenge-128/sgreen/README.md
index a8fab60739..93e51b94d6 100644
--- a/challenge-128/sgreen/README.md
+++ b/challenge-128/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 127
+# The Weekly Challenge 128
-Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-127-4k6j)
+Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-128-1ng1)
diff --git a/challenge-128/sgreen/blog.txt b/challenge-128/sgreen/blog.txt
new file mode 100644
index 0000000000..af278f0388
--- /dev/null
+++ b/challenge-128/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-128-1ng1
diff --git a/challenge-128/sgreen/perl/ch-1.pl b/challenge-128/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..29a74f521e
--- /dev/null
+++ b/challenge-128/sgreen/perl/ch-1.pl
@@ -0,0 +1,71 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+sub _process_input {
+ my @rows = @_;
+ my @cells = ();
+
+ # Convert rows of x and * to an array of arrays
+ foreach my $row (<>) {
+
+ # Skip empty rows
+ next if $row =~ /^\s+$/;
+ push @cells, [ $row =~ /([01])/gm ];
+ }
+
+ # Check we have at least one row
+ die "You must specify at least one row\n" unless scalar(@cells);
+
+ # Check all rows are the same length
+ foreach my $i ( 1 .. $#cells ) {
+ die "Not all rows are the same length\n" if scalar( @{ $cells[0] } ) != scalar( @{ $cells[$i] } );
+ }
+
+ return @cells;
+}
+
+sub main {
+ # Unlike most tasks, we will read the input from STDIN.
+ my @cells = _process_input();
+
+ my $rows = $#cells;
+ my $cols = $#{ $cells[0] };
+ my $best_width = my $best_height = 0;
+
+ # Inspect each cell as the top left of the inner matrix
+ foreach my $y ( 0 .. $rows ) {
+ foreach my $x ( 0 .. $cols ) {
+ # Skip cells that have a one
+ next if $cells[$y][$x] == 1;
+
+ my $max_width = $cols - $x;
+
+ foreach my $height ( 0 .. $rows - $y ) {
+ last if $cells[ $y + $height ][$x] == 1;
+
+ my $width = 0;
+ foreach my $this_width ( 1 .. $max_width ) {
+ last if $cells[ $y + $height ][ $x + $this_width ] == '1';
+ $max_width = $this_width;
+ ++$width;
+ }
+
+ # This is matrix bigger than what we've already found
+ if ( ( $best_width * $best_height ) < ( ( $width + 1 ) * ( $height + 1 ) ) ) {
+ $best_width = $width + 1;
+ $best_height = $height + 1;
+ }
+ }
+ }
+ }
+
+ # Display the result
+ my $row = '[ ' . ( '0 ' x $best_width ) . ']';
+ say $row foreach ( 1 .. $best_height );
+
+}
+
+main();
diff --git a/challenge-128/sgreen/perl/ch-2.pl b/challenge-128/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..461410b728
--- /dev/null
+++ b/challenge-128/sgreen/perl/ch-2.pl
@@ -0,0 +1,51 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use List::Util 'max';
+
+sub _make_time {
+ # Turn a HH:MM time into minutes since midnight
+ my ( $hour, $min ) = split /:/, $_[0], 2;
+ return $hour * 60 + $min;
+}
+
+sub main {
+ # Get the times from the input
+ my @times = ( join( ' ', @_ ) =~ /((?:2[0-3]|[01][0-9]):[0-5][0-9])/g );
+
+ # Sanity check
+ die "You must provide an even amount of times" if $#times % 2 == 0;
+
+ # Pair up the arrival and departure times
+ my $offset = scalar(@times) / 2;
+ my @trains = ();
+ for my $pairs ( 0 .. $#times / 2 ) {
+ my $arr = _make_time( $times[$pairs] );
+ my $dep = _make_time( $times[ $pairs + $offset ] );
+
+ if ( $dep < $arr ) {
+ # The train spans across midnight, so add two entries
+ # There are 1440 minutes in a day, so 1439 is 11:59pm
+ push @trains, [ 0, $arr ], [ $dep, 1439 ];
+ }
+ else {
+ push @trains, [ $arr, $dep ];
+ }
+ }
+
+ # For each minute, figure out how many platforms are used
+ my @platforms = ( (0) x 1440 );
+ foreach my $train (@trains) {
+ foreach my $time ( $train->[0] .. $train->[1] ) {
+ $platforms[$time]++;
+ }
+ }
+
+ # Display the maximum number of platforms required
+ my $max_platforms = max(@platforms);
+ say $max_platforms;
+}
+
+main(@ARGV);
diff --git a/challenge-128/sgreen/perl/example1.txt b/challenge-128/sgreen/perl/example1.txt
new file mode 100644
index 0000000000..109dd5b3d6
--- /dev/null
+++ b/challenge-128/sgreen/perl/example1.txt
@@ -0,0 +1,3 @@
+[ 1 0 0 0 1 0 ]
+[ 1 1 0 0 0 1 ]
+[ 1 0 0 0 0 0 ]
diff --git a/challenge-128/sgreen/perl/example2.txt b/challenge-128/sgreen/perl/example2.txt
new file mode 100644
index 0000000000..3ee68404d8
--- /dev/null
+++ b/challenge-128/sgreen/perl/example2.txt
@@ -0,0 +1,3 @@
+[ 0 0 1 1 ]
+[ 0 0 0 1 ]
+[ 0 0 1 0 ]