Samwell Tarly's Cards

2.8

10 votes
Problem

Samwell is bored while going to his medical school in Oldtown. He has a deck of cards that John had gifted him. He starts to play the following game. The rules for his game is simple.

He deals cards one by one in a row from left to right. He does not overlap his cards. Whenever the card matches its immediate neighbour on the left, or matches the third card to the left, he may move it onto that card. He can match the cards if they are of the same suit or same number.

After making a move, Samwell looks to see if it has made additional moves possible. He can move only the top card of each pile at any given time. He must close up the gaps between piles as soon as they appear by moving all piles on the right of the gap one position to the left. He has to deal out the whole pack, combining cards towards the left whenever possible. He wins the game if the pack is reduced to a single pile.

Sometimes Samwell has more than one play that is possible. Where two cards may be moved, He adopts the strategy of always moving the leftmost card possible.

Input:

Input data to the program specifies the order in which cards are dealt from the pack.

The input contains pairs of lines, each line containing 26 cards separated by single space characters.

The final line of the input file contains a # as its first character.

Cards are represented as a two character code. The first character is the face-value (A=Ace, 2-9, T=10, J=Jack, Q=Queen, K=King) and the second character is the suit (C=Clubs, D=Diamonds, H=Hearts, S=Spades).

There will be at most 10 test cases.

Output:

One line of output must be produced for each pair of lines (that between them describe a pack of 52 cards) in the input.

First Number denotes number of piles and rest of output shows the number of cards in each of the pile remaining after playing the game with the pack of cards as described by the corresponding pairs of input lines.

Note: Samwell suggests you to play this game.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Simulation of 2nd test case:

Firstly, he moves 2C to AC, then 3C to 2C and so on till:

Pile1: AC,2C,3C,4C,5C,6C,7C,8C,9C,TC,JC,QC,KC

Other Piles: AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KD AH 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS

He does the same thing with the diamonds

Pile1: AC,2C,3C,4C,5C,6C,7C,8C,9C,TC,JC,QC,KC

Pile 2: AD,2D,3D,4D,5D,6D,7D,8D,TD,9D,JD,QD,KD

Cards to be dealt: AH 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS

Now he moves Pile of KD to KC

Pile 1: AC,2C,3C,4C,5C,6C,7C,8C,9C,TC,JC,QC,KC,AD,2D,3D,4D,5D,6D,7D,8D,TD,9D,JD,QD,KD

Cards to be dealt: AH 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS

Next Move, Pile up all the Hearts and put it to KD pile

Pile 1: AC,2C,3C,4C,5C,6C,7C,8C,9C,TC,JC,QC,KC,AD,2D,3D,4D,5D,6D,7D,8D,TD,9D,JD,QD,KD, AH,2H,3H,4H,5H,6H,7H,8H,9H,KH

Cards to be dealt: 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS

Next he moves TH to KH pile

Pile 1: AC,2C,3C,4C,5C,6C,7C,8C,9C,TC,JC,QC,KC,AD,2D,3D,4D,5D,6D,7D,8D,TD,9D,JD,QD,KD, AH,2H,3H,4H,5H,6H,7H,8H,9H,KH,TH

Other Piles: 6S QH AS

Cards to be dealt: 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS

Next he moves 2S to 6S (Note that he didn't move it to AS), 3S to 2S and so on:

Pile 1: AC,2C,3C,4C,5C,6C,7C,8C,9C,TC,JC,QC,KC,AD,2D,3D,4D,5D,6D,7D,8D,TD,9D,JD,QD,KD, AH,2H,3H,4H,5H,6H,7H,8H,9H,KH,TH

Pile 2: 6S,2S,3S,4S,5S,QH

Pile 3: AS

Cards to be dealt: JH 7S 8S 9S TS JS QS KS

Next Move JH to TH and QH pile to JH

Pile 1: AC,2C,3C,4C,5C,6C,7C,8C,9C,TC,JC,QC,KC,AD,2D,3D,4D,5D,6D,7D,8D,TD,9D,JD,QD,KD, AH,2H,3H,4H,5H,6H,7H,8H,9H,KH,TH,JH,6S,2S,3S,4S,5S,QH

Pile 2: AS

Cards to be dealt: 7S 8S 9S TS JS QS KS

Next he groups all Spades till QS

Pile 1: AC,2C,3C,4C,5C,6C,7C,8C,9C,TC,JC,QC,KC,AD,2D,3D,4D,5D,6D,7D,8D,TD,9D,JD,QD,KD, AH,2H,3H,4H,5H,6H,7H,8H,9H,KH,TH,JH,6S,2S,3S,4S,5S,QH

Pile 2: AS,7S,8S,9S,TS,JS,QS

Cards to be dealt: KS

After this he will move pile of QS to QH and KS to QS

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

Editor Image

?