Amplitude Hackathon Winter '24 Problem 7 - The Gang

View as PDF

Submit solution

Points: 10 (partial)
Time limit: 0.5s
Memory limit: 1G

Problem type

It's game night at Amplitude, which means a group of individuals are playing The Gang!

This version of The Gang is played with 52 cards. Each card has a suit and a value. The four different suits are clubs (C), diamonds (D), hearts (H), and spades (S). The thirteen different values are ace (A), king (K), queen (Q), jack (J), ten (T), nine (9), eight (8), seven (7), six (6), five (5), four (4), three (3), and two (2). These values are described in decreasing order of value. Of the 52 cards with a suit and a value, each pair of suit and value appears on exactly one card.

Each player is dealt two cards privately, and five community cards are dealt out. Each player therefore has access to seven cards, and out of all possible five-card hands, writes down the strongest possible hand they can make.

The different types of hands that a player can have are, in decreasing order of strength:

  • Straight flush - the five cards form a straight and a flush, the definitions of both are provided below. If two hands are both straight flushes, then they are compared by considering the strength rules for straights.
  • Four of a kind - four of the cards have the same value. If two hands are both four of a kind, then the one with the higher value that appears four times is stronger. Otherwise, the one with the higher value in the single leftover card is stronger. Therefore, 5C 5D 5H 5S 4S beats 3C 3D 3H 3S AC, and 5C 5D 5H 5S AS beats 5C 5D 5H 5S 3C.
  • Full house - exactly three of the cards have the same value, and the other two cards also have the same value. If two hands are both full houses, then the one with the higher value that appears three times is stronger. Otherwise, the one with the higher value that appears twice is stronger. Therefore, QC QD QH 2C 2D beats JC JD JS AC AD, and 9C 9D 9S 8H 8D beats 9C 9H 9S 2C 2S.
  • Flush - all five cards have the same suit. If two hands are both flushes, then we apply high card rules to identify the stronger hand, the definition of which is provided below.
  • Straight - all five cards have different values, and the five values are adjacent. The legal straights in decreasing order of strength are AKQJT, KQJT9, QJT98, JT987, T9876, 98765, 87654, 76543, 65432, and 5432A.
  • Three of a kind - exactly three of the cards have the same value. If two hards are both three of a kind, then the one with the higher value that appears three times is stronger. Otherwise, we take the two remaining cards and apply high card rules.
  • Two pair - two of the cards have the same value, and two different cards have the same value. If two hands are both two pair, then we apply high card rules using the values that appear twice in both hands. If that results in a tie, then we apply high card rules to the remaining card in both hands.
  • One pair - two of the cards have the same value. If two hands are both one pair, the one with the higher value that appears twice is stronger. Otherwise, we apply high card rules on the three remaining cards.
  • High card - all remaining hands are considered high card hands and strictly weaker than all hands that can be identified by one of the above. Take the highest value card from both hands, and the one with the higher value is considered stronger. Otherwise, remove both cards and repeat.

Players are ranked by the strength of the strongest hands they can make. Determine, given the game state, the ranking of all players.

Constraints

2 \le n \le 23

Input Specification

The first line of input contains a single integer, n.

The next n lines of input each contain the description of two cards.

The last line of input contains the description of five cards.

Each card description will be a two-character string where the first character is the value of the card and the second character is the suit of the card.

All cards present will be pairwise distinct.

Output Specification

Output k lines, where k is the number of distinguishable hand strengths among the n players.

Within a line, output player numbers in increasing order. If two player numbers are on the same line, then their strongest hands must be equal in strength.

If line a comes before line b, then every hand from a player in line a must be weaker than any hand from a player in line b.

Sample Input 1

2
3C 4H
AC 9H
7H 6S 5D 9C AD

Sample Output 1

2
1

Sample Explanation 1

Player 1 has a straight as their strongest hand, and player 2 has a two pair as their strongest hand.

Sample Input 2

3
9H 9D
3H 8D
3D 8S
4H 5H 6H 7H 8H

Sample Output 2

2 3
1

Sample Explanation 2

Players 2 and 3 have a straight flush as their highest hand. Player 1 also has a straight flush, but has a stronger one than players 2 and 3.


Comments

There are no comments at the moment.