No friends among the champions
Weekly challenge 343 — 13 October 2025
Week 343: 13 Oct 2025
You have n teams in a tournament. A matrix grid tells you which team is stronger between any two teams:
Find the champion team - the one with most wins, or if there is no single such team, the strongest of the teams with most wins. You may assume that there is a definite answer.
Example 1 Input: @grid = ( [0, 1, 1], [0, 0, 1], [0, 0, 0], ) Output: Team 0 [0, 1, 1] => Team 0 beats Team 1 and Team 2 [0, 0, 1] => Team 1 beats Team 2 [0, 0, 0] => Team 2 loses to all Example 2 Input: @grid = ( [0, 1, 0, 0], [0, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0], ) Output: Team 3 [0, 1, 0, 0] => Team 0 beats only Team 1 [0, 0, 0, 0] => Team 1 loses to all [1, 1, 0, 0] => Team 2 beats Team 0 and Team 1 [1, 1, 1, 0] => Team 3 beats everyone Example 3 Input: @grid = ( [0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 0, 0], [0, 0, 1, 0], ) Output: Team 0 [0, 1, 0, 1] => Team 0 beats teams 1 and 3 [0, 0, 1, 1] => Team 1 beats teams 2 and 3 [1, 0, 0, 0] => Team 2 beats team 0 [0, 0, 1, 0] => Team 3 beats team 2 Of the teams with 2 wins, Team 0 beats team 1. Example 4 Input: @grid = ( [0, 1, 1], [0, 0, 0], [0, 1, 0], ) Output: Team 0 [0, 1, 1] => Team 0 beats Team 1 and Team 2 [0, 0, 0] => Team 1 loses to Team 2 [0, 1, 0] => Team 2 beats Team 1 but loses to Team 0 Example 5 Input: @grid = ( [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], ) Output: Team 2 [0, 0, 0, 0, 0] => Team 0 loses to all [1, 0, 0, 0, 0] => Team 1 beats only Team 0 [1, 1, 0, 1, 1] => Team 2 beats everyone except self [1, 1, 0, 0, 0] => Team 3 loses to Team 2 [1, 1, 0, 1, 0] => Team 4 loses to Team 2
I have - as I usually do - chosen a solution that can be reasonably easily understood at a glance.
First I loop over the teams to see whether the current team has more wins than any preceding team. If so, it is the winner so far.
If it has fewer wins than all the teams so far it definitely isn't the winner.
If it has the same number of wins as the winner so far then it is the current winner if it won the game over the previous winner.
#!/usr/bin/perl # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge use v5.26; # The Weekly Challenge - 2025-10-13 use utf8; # Week 343 - task 2 - Champion team use warnings; # Peter Campbell Smith binmode STDOUT, ':utf8'; use Encode; champion_team([[0, 1, 1], [0, 0, 1], [0, 0, 0]]); champion_team([[0, 1, 0, 0], [0, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0]]); champion_team([[0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 0, 0], [0, 0, 1, 0]] ); champion_team([[0, 1, 1], [0, 0, 0], [0, 1, 0]] ); champion_team([[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]] ); sub champion_team { my ($m, $n, $most_wins, $team, $result, $wins, $winner); # initialise $m = $_[0]; $n = @{$m} - 1; $most_wins = -1; # loop over teams for $team (0 .. $n) { $wins = 0; # loop over results for total wins for $result (0 .. $n) { $wins += $m->[$team]->[$result]; } # if this is best so far if ($wins > $most_wins) { $winner = $team; $most_wins = $wins; # if this equals best so far and this team # beat the current winner } elsif ($wins == $most_wins and $m->[$team]->[$winner]) { $winner = $team; } } # report say ''; print_matrix(qq[Input: ], $m); say qq[Output: winner is team $winner with $most_wins wins]; } sub print_matrix { my ($legend, $matrix, $j); # format matrix ($legend, $matrix) = @_; for $j (0 .. @$matrix - 1) { print qq{$legend [} . join(', ', @{$matrix->[$j]}) . qq(]); say $j == @$matrix - 1 ? '' : ', '; $legend = ' ' x length($legend); } }
Input: [0, 1, 1], [0, 0, 1], [0, 0, 0] Output: winner is team 0 with 2 wins Input: [0, 1, 0, 0], [0, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0] Output: winner is team 3 with 3 wins Input: [0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 0, 0], [0, 0, 1, 0] Output: winner is team 0 with 2 wins Input: [0, 1, 1], [0, 0, 0], [0, 1, 0] Output: winner is team 0 with 2 wins Input: [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] Output: winner is team 2 with 4 wins
Any content of this website which has been created by Peter Campbell Smith is in the public domain