aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-07-02 12:47:16 +0100
committerGitHub <noreply@github.com>2024-07-02 12:47:16 +0100
commit19f14cae53014f5dd00bff6b54b31eba4f89fadf (patch)
treed7751213b3407a1fedd6d94d4f016b17672f7ab6
parenta11e0933ddc1e2efd777a52120b2fb300a389d11 (diff)
parent5e1454423e3c9a426334684858eb9c43cc9b46bd (diff)
downloadperlweeklychallenge-club-19f14cae53014f5dd00bff6b54b31eba4f89fadf.tar.gz
perlweeklychallenge-club-19f14cae53014f5dd00bff6b54b31eba4f89fadf.tar.bz2
perlweeklychallenge-club-19f14cae53014f5dd00bff6b54b31eba4f89fadf.zip
Merge pull request #10358 from choroba/ech276
Add solutions to 276: Complete Day & Maximum Frequency by E. Choroba
-rwxr-xr-xchallenge-276/e-choroba/perl/ch-1.pl58
-rwxr-xr-xchallenge-276/e-choroba/perl/ch-2.pl22
2 files changed, 80 insertions, 0 deletions
diff --git a/challenge-276/e-choroba/perl/ch-1.pl b/challenge-276/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..664e1480b4
--- /dev/null
+++ b/challenge-276/e-choroba/perl/ch-1.pl
@@ -0,0 +1,58 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+# Optimised solution, much faster for large input arrays.
+sub complete_day(@hours) {
+ my %modulo24;
+ ++$modulo24{ $_ % 24 } for @hours;
+ my $tally = 0;
+ for my $x (1 .. 11) {
+ $tally += ($modulo24{$x} // 0) * ($modulo24{ 24 - $x } // 0);
+ }
+ for my $x (0, 12) {
+ $tally += ($modulo24{$x} ** 2 - $modulo24{$x}) / 2
+ if exists $modulo24{$x};
+ }
+ return $tally
+}
+
+sub complete_day_simple(@hours) {
+ my $tally = 0;
+ for my $i (0 .. $#hours - 1) {
+ for my $j ($i + 1 .. $#hours) {
+ ++$tally if ($hours[$i] + $hours[$j]) % 24 == 0;
+ }
+ }
+ return $tally
+}
+
+use Test::More tests => 3 + 7 + 1001;
+
+is complete_day(12, 12, 30, 24, 24), 2, 'Example 1';
+is complete_day(72, 48, 24, 5), 3, 'Example 2';
+is complete_day(12, 18, 24), 0, 'Example 3';
+
+is complete_day(1 .. 23), 11, '1..23';
+is complete_day(12, 1 .. 23), 12, '12, 1..23';
+is complete_day(1 .. 36), 23, '1..36';
+is complete_day(1 .. 37), 25, '1..37 (introduce 72)';
+is complete_day(1 .. 47), 45, '1..47';
+is complete_day(1 .. 60), 70, '1..60';
+is complete_day(1 .. 61), 73, '1..61 (introduce 96)';
+
+for (1 .. 1000) {
+ my @hours = map int rand 100, 1 .. 10 + rand 100;
+ is complete_day(@hours), complete_day_simple(@hours), "same $_";
+}
+
+use Benchmark qw{ cmpthese };
+
+my @hours = map int rand 100, 1 .. 10 + rand 100;
+is complete_day(@hours), complete_day_simple(@hours), "same bench (@hours)";
+
+cmpthese(-3, {
+ simple => sub { complete_day_simple(@hours) },
+ optimised => sub { complete_day(@hours) },
+});
diff --git a/challenge-276/e-choroba/perl/ch-2.pl b/challenge-276/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..2a71066c52
--- /dev/null
+++ b/challenge-276/e-choroba/perl/ch-2.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+use List::Util qw{ max };
+
+sub maximum_frequency(@ints) {
+ my %freq;
+ ++$freq{$_} for @ints;
+ my %freq_freq;
+ ++$freq_freq{$_} for values %freq;
+ my $max_freq = max(keys %freq_freq);
+ return $freq_freq{$max_freq} * $max_freq
+}
+
+use Test::More tests => 2 + 1;
+
+is maximum_frequency(1, 2, 2, 4, 1, 5), 4, 'Example 1';
+is maximum_frequency(1, 2, 3, 4, 5), 5, 'Example 2';
+
+is maximum_frequency((1 .. 3) x 4), 12, '3x4';