aboutsummaryrefslogtreecommitdiff
path: root/challenge-226/dave-jacoby/perl/ch-2.pl
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-226/dave-jacoby/perl/ch-2.pl')
-rw-r--r--challenge-226/dave-jacoby/perl/ch-2.pl31
1 files changed, 31 insertions, 0 deletions
diff --git a/challenge-226/dave-jacoby/perl/ch-2.pl b/challenge-226/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..ac9dfe74a5
--- /dev/null
+++ b/challenge-226/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+
+use List::Util qw{ sum sum0 min max };
+
+my @examples = ( [ 1, 5, 0, 3, 5 ], [0], [ 2, 1, 4, 0, 3 ], );
+
+for my $e (@examples) {
+ my $input = join ',', $e->@*;
+ my $output = zero_array( $e->@* );
+ say <<~"END";
+ Input: \@ints = ($input)
+ Output: $output
+ END
+}
+
+sub zero_array( @ints ) {
+ my $c = -1;
+ while (1) {
+ $c++;
+ my $min = min grep { $_ > 0 } @ints;
+ $min //= 0;
+ @ints = map { $_ - $min > 0 ? $_ - $min : 0 } @ints;
+ last if $min == 0;
+ last if $c > 10;
+ }
+ return $c;
+}