From c86ef9be7e90ef310b563308dc1cb3260d41ab5f Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 13 Oct 2025 10:08:04 +0200 Subject: Solution to task 2 --- challenge-343/jo-37/perl/ch-2.pl | 114 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100755 challenge-343/jo-37/perl/ch-2.pl diff --git a/challenge-343/jo-37/perl/ch-2.pl b/challenge-343/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..a5d9e1464b --- /dev/null +++ b/challenge-343/jo-37/perl/ch-2.pl @@ -0,0 +1,114 @@ +#!/usr/bin/perl + +use v5.26; +use Test2::V0 -no_srand; +use Test2::Tools::Subtest 'subtest_streamed'; +use Getopt::Long; +use experimental 'signatures'; + +use List::Util 'sum'; + +### Options and Arguments + +my ($tests, $examples, $verbose); +GetOptions( + 'examples!' => \$examples, + 'tests!' => \$tests, + 'verbose!' => \$verbose, +) or usage(); + +run_tests($examples, $tests); # tests do not return + +usage() unless @ARGV; + +sub usage { + die <<~EOS; + $0 - champion team + + usage: $0 [-examples] [-tests] [GRID] + + -examples + run the examples from the challenge + + -tests + run some tests + + GRID + matrix representing the results of all team pairings + in the form R00,R01,R02,... R10,R11,R12,... ... + EOS +} + + +### Input and Output + +say champion_team(map [split ',', $_], @ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/10/17/ch-343.html#task-2 + +sub champion_team { + my @wins = map {sum @$_} @_; + (sort {$wins[$a] <=> $wins[$b] || 2 * $_[$a][$b] - 1} 0 .. $#_)[-1]; +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name) { + my $result = champion_team(@$args); + is $result, $expected, + "$name: (@{[map qq{[@$_]}, @$args]}) -> $expected"; + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [[[0, 1, 1], + [0, 0, 1], + [0, 0, 0]], 0, 'example 1'], + [[[0, 1, 0, 0], + [0, 0, 0, 0], + [1, 1, 0, 0], + [1, 1, 1, 0]], 3, 'example 2'], + [[[0, 1, 0, 1], + [0, 0, 1, 1], + [1, 0, 0, 0], + [0, 0, 1, 0]], 0, 'example 3'], + [[[0, 1, 1], + [0, 0, 0], + [0, 1, 0]], 0, 'example 4'], + [[[0, 0, 0, 0, 0], + [1, 0, 0, 0, 0], + [1, 1, 0, 1, 1], + [1, 1, 0, 0, 0], + [1, 1, 0, 1, 0]], 2, 'example 5'], + ); + plan scalar @examples; + for (@examples) { + run_example @$_; + } + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + my @tests = ( + [[[0, 1, 1, 0], + [0, 0, 1, 0], + [0, 0, 0, 0], + [1, 0, 0, 0]], 0, '3 beats 0'], + ); + plan scalar @tests; + for (@tests) { + run_example @$_; + } + }) : pass 'skip tests'; + + exit; +} -- cgit