aboutsummaryrefslogtreecommitdiff
path: root/challenge-271/dave-jacoby/perl/ch-1.pl
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-271/dave-jacoby/perl/ch-1.pl')
-rw-r--r--challenge-271/dave-jacoby/perl/ch-1.pl40
1 files changed, 40 insertions, 0 deletions
diff --git a/challenge-271/dave-jacoby/perl/ch-1.pl b/challenge-271/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..7427d07108
--- /dev/null
+++ b/challenge-271/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ bitwise fc postderef say signatures state };
+
+use List::Util qw{max};
+
+my @examples = (
+
+ [ [ 0, 1 ], [ 1, 0 ], ],
+ [ [ 0, 0, 0 ], [ 1, 0, 1 ], ],
+ [ [ 0, 0 ], [ 1, 1 ], [ 0, 0 ], ],
+);
+for my $example (@examples) {
+ use JSON;
+ my $j = JSON->new->pretty->canonical;
+ my $output = maximum_ones($example);
+ my $input = display_matrix($example);
+ say <<"END";
+ Input: \$matrix =
+ [ $input ]
+ Output: $output
+END
+}
+
+sub maximum_ones ($matrix) {
+ my @rows = (0);
+ for my $r ( 1 .. scalar @$matrix ) {
+ $rows[$r] = scalar grep { $_ == 1 } $matrix->[ $r - 1 ]->@*;
+ }
+ my $max = max @rows;
+ my ($i) = grep { $rows[$_] == $max } 0 .. -1 + scalar @rows;
+ return $i;
+}
+
+sub display_matrix ($matrix) {
+ return join ",\n ",
+ map { join ' ', '[', ( join ', ', $_->@* ), ']' } $matrix->@*;
+}