diff options
| -rw-r--r-- | challenge-276/dave-jacoby/perl/ch-1.pl | 35 | ||||
| -rw-r--r-- | challenge-276/dave-jacoby/perl/ch-2.pl | 31 |
2 files changed, 66 insertions, 0 deletions
diff --git a/challenge-276/dave-jacoby/perl/ch-1.pl b/challenge-276/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..93b58c6871 --- /dev/null +++ b/challenge-276/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ bitwise fc postderef say signatures state }; + +use List::Util qw{max}; + +my @examples = ( + + [ 12, 12, 30, 24, 24 ], + [ 72, 48, 24, 5 ], + [ 12, 18, 24 ], +); + +for my $example (@examples) { + my $output = complete_day($example); + my $input = join ', ', $example->@*; + say <<~"END"; + Input: \@hours = ($input) + Output: $output + END +} + +sub complete_day ($durations) { + my @output; + my @durations = $durations->@*; + for my $i ( 0 .. -1 + scalar @durations ) { + for my $j ( $i + 1 .. -1 + scalar @durations ) { + my $d = $durations[$i] + $durations[$j]; + push @output, [ $durations[$i], $durations[$j] ] if $d % 24 == 0; + } + } + return scalar @output; +} diff --git a/challenge-276/dave-jacoby/perl/ch-2.pl b/challenge-276/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..fc837e272d --- /dev/null +++ b/challenge-276/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ fc say postderef signatures state }; + +use List::Util qw{ max }; + +my @examples = ( + +[1, 2, 2, 4, 1, 5], +[1, 2, 3, 4, 5], +); + +for my $input (@examples) { + my $output = max_freq($input->@*); + my $ints = join ', ', $input->@*; + + say <<"END"; + Input: \@ints = ($ints) + Output: $output +END +} + +sub max_freq ( @ints ) { + my %hash ; + map { $hash{$_}++ } @ints; + my $max = max values %hash; + my @keys = grep { $hash{$_} == $max } keys %hash; + return $max * scalar @keys; +}
\ No newline at end of file |
