Camel
Peter
Peter Campbell Smith

Sequence and consequence

Weekly challenge 356 — 12 January 2026

Week 356: 12 Jan 2026

Task 2

Task — Who wins

It’s NFL playoff time! Since the 2020 season, seven teams from each of the league’s two conferences (AFC and NFC) qualify for the playoffs based on regular season winning percentage, with a tie-breaking procedure if required.

The top team in each conference receives a first-round bye, automatically advancing to the second round.

The following games are played. Sometimes the games are played in a different order. To make things easier, assume the order is always as below.

Week 1: Wild card playoffs

  • Team 1 gets a bye.
  • Game 1: Team 2 hosts Team 7.
  • Game 2: Team 3 hosts Team 6.
  • Game 3: Team 4 hosts Team 5.

Week 2: Divisional playoffs

  • Game 4: Team 1 hosts the third seeded winner from the previous week.
  • Game 5: The highest seeded winner from the previous week hosts the second seeded winner.

Week 3: Conference final

  • Game 6: The highest seeded winner from the previous week hosts the other winner.

You are given a six character string containing only H (home) and A (away) which indicates the winner of each game.

Which two teams competed in the the conference final and who won?

Examples


Example 1
NFC Conference 2024/5. Teams were Detroit, Philadelphia,
   Tampa Bay, Los Angeles Rams, Minnesota,
   Washington and Green Bay. Philadelphia - seeded second 
   - won.
Input: $results = 'HAHAHH'
Output: 'Team 2 defeated Team 6'

Example 2
AFC Conference 2024/5. Teams were Kansas City, Buffalo,
   Baltimore, Houston, Los Angeles Charges,
   Pittsburgh and Denver. Kansas City - seeded first - 
   won.
Input: $results = 'HHHHHH'
Output: 'Team 1 defeated Team 2'

Example 3
AFC Conference 2021/2. Teams were Tennessee, Kansas City,
   Buffalo, Cincinnati, Las Vegas,
   New England and Pittsburgh. Cincinnati - seeded fourth 
   - won.
Input: $results = 'HHHAHA'
Output: 'Team 4 defeated Team 2'

Example 4
NFC Conference 2021/2. Teams were Green Bay, Tampa Bay,
   Dallas, Los Angeles Rams, Arizona,
   San Francisco and Philadelphia. The Rams - seeded 
   fourth - won.
Input: $results = 'HAHAAH'
Output: 'Team 4 defeated Team 6'

Example 5
NFC Conference 2020/1. Teams were Green Bay, New Orleans,
   Seattle, Washington, Tampa Bay,
   Los Angeles Rams and Chicago. Tampa Bay - seeded fifth 
   - won.
Input: $results = 'HAAHAA'
Output: 'Team 5 defeated Team 1'

Analysis

Welcome to the world of professional (American) football!

I made the assumption that the teams are seeded in the order listed and that 'higher' seed means the lower-numbered seed.

It's then one line of code to determine the winner of each game. The slight twist is that before each round the teams have to be sorted in seed order so that we know which is the home team.

Also, to cope with team 1 getting a bye into the 2nd round I invented a game 0 which team 1 wins.

You can see how this year's playoffs are going here.

Try it 

Try running the script with any input:



example: One, Two, Three, Four, Five, Six, Seven



example: HAHAHA

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2026-01-12
use utf8;     # Week 356 - task 2 - Who wins
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

who_wins(['Detroit', 'Philadelphia', 'Tampa Bay', 
    'Los Angeles Rams', 'Minnesota', 'Washington', 
    'Green Bay'], 'HAHAHH');
    
who_wins(['Kansas City', 'Buffalo', 'Baltimore', 
    'Houston', 'Los Angeles Chargers', 'Pittsburgh', 
    'Denver'], 'HHHHHH');
    
who_wins(['Tennessee', 'Kansas City', 'Buffalo', 
    'Cincinnati', 'Las Vegas', 'New England', 
    'Pittsburgh'], 'HHHAHA');
    
who_wins(['Green Bay', 'Tampa Bay', 'Dallas',
    'Los Angeles Rams', 'Arizona', 'San Francisco',
    'Philadelphia'], 'HAHAAH');
    
who_wins(['Green Bay', 'New Orleans', 'Seattle',
    'Washington', 'Tampa Bay', 'Los Angeles Rams', 
    'Chicago'], 'HAAHAA');

sub who_wins {
    
    my (@teams, @results, @winner);
    
    # initialise
    @teams = @{$_[0]};
    unshift(@teams, '');
    @results = split('', qq[x$_[1]]);
    
    # wild card playoffs
    $winner[0] = 1;
    $winner[1] = $results[1] eq 'H' ? 2 : 7;
    $winner[2] = $results[2] eq 'H' ? 3 : 6;
    $winner[3] = $results[3] eq 'H' ? 4 : 5;
    
    # divisional playoffs
    @winner = sort {$a <=> $b} @winner;
    $winner[4] = $results[4] eq 'H' ? $winner[0] : $winner[3];
    $winner[5] = $results[5] eq 'H' ? $winner[1] : $winner[2];
    
    # final
    @winner = sort {$a <=> $b} @winner[4 .. 5];
    $winner[6] = $results[6] eq 'H' ? $winner[0] : $winner[1];
        
    # ... and the winnner is ...
    say qq[\nInput:  \@teams = ] . join(', ', @teams[1 .. 7]) .
        qq[\n        \$results = ] . join('', @results[1 .. 6]);
    say qq[Output: $teams[$winner[0]] played $teams[$winner[1]]] .
        qq[ and $teams[$winner[6]] won];
}


Output


Input:  @teams = Detroit, Philadelphia, Tampa Bay,
   Los Angeles Rams, Minnesota, Washington, Green Bay
        $results = HAHAHH
Output: Philadelphia played Washington and Philadelphia 
   won

Input:  @teams = Kansas City, Buffalo, Baltimore, Houston,
   Los Angeles Chargers, Pittsburgh, Denver
        $results = HHHHHH
Output: Kansas City played Buffalo and Kansas City won

Input:  @teams = Tennessee, Kansas City, Buffalo,
   Cincinnati, Las Vegas, New England, Pittsburgh
        $results = HHHAHA
Output: Kansas City played Cincinnati and Cincinnati won

Input:  @teams = Green Bay, Tampa Bay, Dallas,
   Los Angeles Rams, Arizona, San Francisco, Philadelphia
        $results = HAHAAH
Output: Los Angeles Rams played San Francisco and Los 
   Angeles Rams won

Input:  @teams = Green Bay, New Orleans, Seattle,
   Washington, Tampa Bay, Los Angeles Rams, Chicago
        $results = HAAHAA
Output: Green Bay played Tampa Bay and Tampa Bay won

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain