Black Jack

No attempts yetTime limit1sMemory limit256 MB

Problem

Black Jack is a card game in which you try to hold cards worth no more than 21. Casinos play it for money, but this version has no money at stake and only counts wins.

A pack holds 52 cards, four of each of 13 values. Cards are worth the following.

  • The picture cards K, Q and J are worth 10.
  • T (the ten) is worth 10.
  • 2 to 9 are worth their face value.
  • A (the ace) is worth 1 or 11.

The rules are these.

  1. In each scenario a sequence of games is played, dealing cards in the order given in the input.
  2. To start a game, each player is dealt one card in the order given in the input, then the dealer is dealt one card. The dealer is not one of the players. A second card is then dealt in the same way.
  3. Players take their turns in that same order. Every decision uses the hand value, which is the sum of the values of the cards in the hand. Each A counts as 11 at first, and later one or more aces may drop to 1. Once an ace has dropped to 1 it stays at 1.
  4. A turn runs like this.
    • If the two opening cards are both aces, one ace drops to 1, so the hand value is 12 and not 22.
    • If the hand value is 17 or more, the player sticks and the turn ends.
    • If the hand value is less than 17, the player is dealt another card. If the hand value is then 22 or more the player has busted and the turn ends, unless at least one ace is still counted as 11. In that case one ace drops to 1 and the turn continues.
    • The turn continues until the player sticks or busts.
  5. Once every player has taken a turn, the dealer plays in the same way if at least one player has not busted.
  6. If the dealer busts, every player who has not busted scores 1 win.
  7. If the dealer sticks, the dealer scores 1 win for every sticking player with a lower hand value, plus 1 win for every busted player. A player who has not busted scores 1 win if that player's hand value is higher than the dealer's. Equal hand values score for nobody.
  8. If every player has busted, the dealer does not take a turn and scores 1 win per player.
  9. When a card has to be dealt and no cards are left in the pack, the game in progress stops and is not scored. The scenario ends at that point and the number of wins for each player is printed.

Input

Several scenarios are given. One scenario takes two lines.

The first line holds the 52 card values of one pack in the order they are dealt, written with no spaces. Each character is one of A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, and each value appears exactly four times.

The second line holds 1 to 5 player names separated by single spaces. A name contains no spaces.

The last line holds # alone. That line is not processed.

Output

Print one line per scenario. Write the player names in the order given in the input, each followed by that player's number of wins, then dealer followed by the dealer's number of wins. Separate names and numbers with single spaces.