Under construction

Still being built. Things may change

Clock Patience

Time limit1sMemory limit128 MB

Summary
Simulate Clock Patience on several decks: deal cards into 13 piles, flip cards following rank-to-pile moves, and report how many cards were exposed and the last card.
Level

Easy3 of 10

Topics
Simulation, Array, Implementation
Solved
No attempts yet

Problem

Clock layout

Card sharp Albert "Foxy" Smith is writing a book on patience (solitaire) games. To double-check the examples in the book, he writes programs that find the optimal play for a given deal. The rules of Clock Patience are as follows.

The cards are dealt face down into a circle that represents a clock, with one pile at each hour position (1 through 12) and an extra pile in the centre. The first card is dealt to the one o'clock pile, the next to two o'clock, and so on clockwise, with every thirteenth card going to the centre pile. This produces thirteen piles, each holding four face-down cards.

The game then begins. The top card of the "king" pile (the last card dealt, in the centre) is turned face up to become the current card. Each move takes the current card, places it face up beneath the pile whose value it matches, and turns over the top card of that pile to become the new current card. For example, if the current card is an Ace it is placed under the "one" pile and the top card of that pile becomes the current card. The game ends when the pile named by the current card has no face-down cards left. You win if the entire deck is exposed.

Write a program that reads several shuffled decks and plays the game for each one.

Input

The input consists of several decks. Each deck is given as four lines of 13 cards, with the cards on a line separated by a single space. Each card is written as two characters: the rank (A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K) followed by the suit (H, D, C, S). The input ends with a line containing a single #. Within a deck the cards are listed from the bottom of the pack to the top, so the first card dealt is the last card listed.

Output

For each deck output one line. The line contains the number of cards exposed during the game, written as two digits with a leading zero if necessary, then a comma, then the last card exposed, written in the same format as the input.

Examples2

  1. Example 1

    Input
    TS QC 8S 8D QH 2D 3H KH 9H 2H TH KS KC
    9D JH 7H JD 2S QS TD 2C 4H 5H AD 4D 5D
    6D 4S 9S 5S 7S JS 8H 3D 8C 3S 4C 6S 9C
    AS 7C AH 6H KD JC 7D AC 5C TC QD 6C 3C
    #
    
    Expected output
    44,KD
    
  2. Example 2

    Input
    JS TH QC TD 3H KC 7D QH TC 6H 4C KS 5S
    3D JD 8S 9S 9D 2S AS KH QS 7C 8D KD AC
    9C JH 8C 4H 6D 2D 5D 6S AH 2C 4S 2H 7S
    7H AD QD 3S 5C 3C 6C 8H 4D 5H TS JC 9H
    #
    
    Expected output
    33,KD