aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE. Choroba <choroba@matfyz.cz>2025-08-25 17:49:52 +0200
committerE. Choroba <choroba@matfyz.cz>2025-08-25 17:49:52 +0200
commita095053e198f584846c14074c73f8fc19d09030e (patch)
tree0dfc1137c4c8374a6bdd1f2671182a93d6f478c2
parent778a877423d75f6299c57bb75c3edffd2f304e84 (diff)
downloadperlweeklychallenge-club-a095053e198f584846c14074c73f8fc19d09030e.tar.gz
perlweeklychallenge-club-a095053e198f584846c14074c73f8fc19d09030e.tar.bz2
perlweeklychallenge-club-a095053e198f584846c14074c73f8fc19d09030e.zip
Add solutions to 336: Equal Group & Final Score by E. Choroba
-rwxr-xr-xchallenge-336/e-choroba/perl/ch-1.pl24
-rwxr-xr-xchallenge-336/e-choroba/perl/ch-2.pl37
2 files changed, 61 insertions, 0 deletions
diff --git a/challenge-336/e-choroba/perl/ch-1.pl b/challenge-336/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..80b711acba
--- /dev/null
+++ b/challenge-336/e-choroba/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+use Math::Prime::Util qw{ gcd };
+
+sub equal_group(@ints) {
+ my %seen;
+ ++$seen{$_} for @ints;
+
+ my $gcd = gcd(values %seen);
+ return 1 == $gcd ? 0 : 1
+}
+
+use Test2::V0;
+use constant { true => bool(1),
+ false => bool(0) };
+plan(5);
+
+is equal_group(1, 1, 2, 2, 2, 2), true, 'Example 1';
+is equal_group(1, 1, 1, 2, 2, 2, 3, 3), false, 'Example 2';
+is equal_group(5, 5, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7), true, 'Example 3';
+is equal_group(1, 2, 3, 4), false, 'Example 4';
+is equal_group(8, 8, 9, 9, 10, 10, 11, 11), true, 'Example 5';
diff --git a/challenge-336/e-choroba/perl/ch-2.pl b/challenge-336/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..7dcee0fca8
--- /dev/null
+++ b/challenge-336/e-choroba/perl/ch-2.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+use List::Util qw{ sum };
+
+sub invalidate($final) { pop @$final }
+sub double($final) { push @$final, 2 * $final->[-1] }
+sub add($final) { push @$final, $final->[-1] + $final->[-2] }
+
+my %DISPATCH = (
+ C => \&invalidate,
+ D => \&double,
+ '+' => \&add,
+);
+
+sub final_score(@scores) {
+ my @final;
+ for my $score (@scores) {
+ if (exists $DISPATCH{$score}) {
+ $DISPATCH{$score}->(\@final);
+ } else {
+ push @final, $score;
+ }
+ }
+ return sum(@final)
+}
+
+use Test::More tests => 5;
+
+is final_score('5', '2', 'C', 'D', '+'), 30, 'Example 1';
+is final_score('5', '-2', '4', 'C', 'D', '9', '+', '+'), 27, 'Example 2';
+is final_score('7', 'D', 'D', 'C', '+', '3'), 45, 'Example 3';
+is final_score('-5', '-10', '+', 'D', 'C', '+'), -55, 'Example 4';
+is final_score('3', '6', '+', 'D', 'C', '8', '+', 'D', '-2', 'C', '+'), 128,
+ 'Example 5';