aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-03-29 16:44:58 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-03-29 16:44:58 +0200
commit76032fdf925fbd00d42b86f1c69e80a6680f3b13 (patch)
treee8cff4da8031e8eab6df04d09a9f0b27cc88daaf
parent8915a66de2cb2a724aee5e55ddfc15580cfdf1d5 (diff)
downloadperlweeklychallenge-club-76032fdf925fbd00d42b86f1c69e80a6680f3b13.tar.gz
perlweeklychallenge-club-76032fdf925fbd00d42b86f1c69e80a6680f3b13.tar.bz2
perlweeklychallenge-club-76032fdf925fbd00d42b86f1c69e80a6680f3b13.zip
Solution to task 1
-rwxr-xr-xchallenge-210/jo-37/perl/ch-1.pl64
1 files changed, 64 insertions, 0 deletions
diff --git a/challenge-210/jo-37/perl/ch-1.pl b/challenge-210/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..8a8aef7f2e
--- /dev/null
+++ b/challenge-210/jo-37/perl/ch-1.pl
@@ -0,0 +1,64 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+use List::Util qw(sum max);
+use List::UtilsBy qw(count_by);
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [--] [N...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+N...
+ list of numbers
+
+EOS
+
+
+### Input and Output
+
+say kill_and_win(@ARGV);
+
+
+### Implementation
+
+# Brute force implementation.
+
+sub kill_and_win {
+ # Create a map from values to their frequencies.
+ my %freq = count_by {$_} @_;
+ # Find the maximum over three consectutive values.
+ max map {
+ sum map {$_ * ($freq{$_} // 0)} $_ - 1, $_, $_ + 1
+ } keys %freq;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is kill_and_win(2, 3, 1), 6, 'example 1';
+ is kill_and_win(1, 1, 2, 2, 2, 3), 11, 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is kill_and_win(1, 1, 2, 2, 3, 3, 13), 13, 'single large value';
+ }
+
+ done_testing;
+ exit;
+}