aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-128/stuart-little/perl/ch-1.pl21
-rwxr-xr-xchallenge-128/stuart-little/perl/ch-2.pl18
2 files changed, 39 insertions, 0 deletions
diff --git a/challenge-128/stuart-little/perl/ch-1.pl b/challenge-128/stuart-little/perl/ch-1.pl
new file mode 100755
index 0000000000..1dfe73a6de
--- /dev/null
+++ b/challenge-128/stuart-little/perl/ch-1.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl
+use warnings;
+use v5.12;
+
+# run <script> <space-separated binary words, with one word representing each row>
+
+use List::Util qw(reduce);
+use List::UtilsBy qw(max_by);
+
+my ($rows,$cols)=(0,0);
+my $digs = length $ARGV[0];
+for my $i (0..scalar @ARGV-1) {
+ for my $j ($i..scalar @ARGV-1) {
+ my $max = max_by { length $_ } split /[^0]+/, reduce { sprintf("%0${digs}b", oct("0b$a") | oct("0b$b")) } @ARGV[$i..$j];
+ if (($j-$i+1) * (length $max) > $rows * $cols) {
+ ($rows,$cols) = ($j-$i+1, length $max);
+ }
+ }
+}
+
+say join "\n", ('0' x $cols) x $rows;
diff --git a/challenge-128/stuart-little/perl/ch-2.pl b/challenge-128/stuart-little/perl/ch-2.pl
new file mode 100755
index 0000000000..ccae73e31b
--- /dev/null
+++ b/challenge-128/stuart-little/perl/ch-2.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/env perl
+use warnings;
+use v5.12;
+
+# run <script> <starting arrivals followed by departures, all space-separated>
+
+use List::Util qw(sum0 zip);
+
+my @times = sort {$a->[0] cmp $b->[0]} map { [$_->[0], $_->[1] % int((scalar @ARGV)/2)] } zip \@ARGV, [0..scalar @ARGV-1];
+my $sol=0;
+my @station = (0) x int((scalar @ARGV)/2);
+
+for (@times) {
+ $station[$_->[1]]^=1;
+ ((sum0 @station) > $sol) && do {$sol = sum0 @station};
+}
+
+say $sol;