The weekly challenge 356 - Task 2: Who Wins
1 #!/usr/bin/env perl 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-356/#TASK2 3 # 4 # Task 2: Who Wins 5 # ================ 6 # 7 # It’s NFL playoff time. Since the 2020 season, seven teams from each of the 8 # league’s two conferences (AFC and NFC) qualify for the playoffs based on 9 # regular season winning percentage, with a tie-breaking procedure if required. 10 # The top team in each conference receives a first-round bye, automatically 11 # advancing to the second round. 12 # 13 # The following games are played. Some times the games are played in a 14 # different order. To make things easier, assume the order is always as below. 15 # 16 # Week 1: Wild card playoffs 17 # 18 # - Team 1 gets a bye 19 # - Game 1: Team 2 hosts Team 7 20 # - Game 2: Team 3 hosts Team 6 21 # - Game 3: Team 4 hosts Team 5 22 # - Week 2: Divisional playoffs 23 # - Game 4: Team 1 hosts the third seeded winner from the previous week. 24 # - Game 5: The highest seeded winner from the previous week hosts the second seeded winner. 25 # - Week 3: Conference final 26 # - Game 6: The highest seeded winner from the previous week hosts the other winner 27 # 28 # You are given a six character string containing only H (home) and A away 29 # which has the winner of each game. Which two teams competed in the the 30 # conference final and who won? 31 # 32 ## Example 1 33 ## NFC Conference 2024/5. Teams were Detroit, Philadelphia, Tampa Bay, Los 34 ## Angeles Rams, Minnesota, Washington and Green Bay. Philadelphia - seeded 35 ## second - won. 36 ## 37 ## Input: $results = "HAHAHH" 38 ## Output: "Team 2 defeated Team 6" 39 ## 40 ## In Week 1, Team 2 (home) won against Team 7, Team 6 (away) defeated Team 3 41 ## and Team 4 (home) were victorious over Team 5. This means the second week 42 ## match ups are Team 1 at home to Team 6, and Team 2 hosted Team 4. 43 ## 44 ## In week 2, Team 6 (away) won against Team 1, while Team 2 (home) beat Team 4. 45 ## The final week was Team 2 hosting Team 6 46 ## 47 ## In the final week, Team 2 (home) won against Team 6. 48 # 49 ## Example 2 50 ## AFC Conference 2024/5. Teams were Kansas City, Buffalo, Baltimore, Houston, 51 ## Los Angeles Charges, Pittsburgh and Denver. Kansas City - seeded first - won. 52 ## 53 ## Input: $results = "HHHHHH" 54 ## Output: "Team 1 defeated Team 2" 55 # 56 ## Example 3 57 ## AFC Conference 2021/2. Teams were Tennessee, Kansas City, Buffalo, 58 ## Cincinnati, Las Vegas, New England and Pittsburgh. Cincinnati - seeded fourth 59 ## - won. 60 ## 61 ## Input: $results = "HHHAHA" 62 ## Output: "Team 4 defeated Team 2" 63 # 64 ## Example 4 65 ## NFC Conference 2021/2. Teams were Green Bay, Tampa Bay, Dallas, Los Angeles 66 ## Rams, Arizona, San Francisco and Philadelphia. The Rams - seeded fourth - 67 ## won. 68 ## 69 ## Input: $results = "HAHAAH" 70 ## Output: "Team 4 defeated Team 6" 71 # 72 ## Example 5 73 ## NFC Conference 2020/1. Teams were Green Bay, New Orleans, Seattle, 74 ## Washington, Tampa Bay, Los Angeles Rams and Chicago. Tampa Bay - seeded fifth 75 ## - won. 76 ## 77 ## Input: $results = "HAAHAA" 78 ## Output: "Team 5 defeated Team 1" 79 # 80 ############################################################ 81 ## 82 ## discussion 83 ## 84 ############################################################ 85 # 86 # Dependent on the results of week one, we set up the matches for 87 # week two. Dependent on those results, we set up the final match. 88 # Then we return the result of that. 89 # 90 use v5.36; 91 92 who_wins("HAHAHH"); 93 who_wins("HHHHHH"); 94 who_wins("HHHAHA"); 95 who_wins("HAHAAH"); 96 who_wins("HAAHAA"); 97 98 sub who_wins($results) { 99 say "Input: $results"; 100 my @games = split //, $results; 101 my @week2_teams = (1); 102 push @week2_teams, $games[0] eq "H" ? 2 : 7; 103 push @week2_teams, $games[1] eq "H" ? 3 : 6; 104 push @week2_teams, $games[2] eq "H" ? 4 : 5; 105 my @week2_teams_sorted = sort { $a <=> $b } @week2_teams; 106 my @finals = (); 107 push @finals, $games[3] eq "H" ? $week2_teams_sorted[0] : $week2_teams_sorted[3]; 108 push @finals, $games[4] eq "H" ? $week2_teams_sorted[1] : $week2_teams_sorted[2]; 109 my @finals_sorted = sort { $a <=> $b } @finals; 110 if($games[5] eq "H") { 111 say "Team $finals_sorted[0] defeated Team $finals_sorted[1]"; 112 } else { 113 say "Team $finals_sorted[1] defeated Team $finals_sorted[0]"; 114 } 115 }