Bulgarian Solitaire is a single-player game made popular by Martin Gardner in his mathematical column in Scientific American.
You have N
identical cards, split into piles. You take a card from each pile and form a new pile with the removed cards. You repeat this process until you reach a state you've already seen and so continuing would repeat the loop.
For example, say you have 8
cards, split into a pile of 5
and a pile of 3
. We write the pile sizes in descending order: 5 3
. Here's a transcript of the game:
5 3
4 2 2
3 3 1 1
4 2 2
You first remove a card from each of the two piles, leaving piles of 4
and 2
, and a newly-created pile of 2
, giving 4 2 2
. In the next step, these decrease to 3 1 1
followed with a new pile of 3
. Finally, the last step empties the piles of size 1
and produces4 2 2
which has already appeared, so we stop.
Note that the sum of the pile-sizes stays the same.
Your goal is to print such a transcript of the game from a given starting configuration. This is code golf, so fewest bytes wins.
Input
A list of positive numbers in descending order representing the initial pile sizes. Take input via STDIN or function input. You can use any list-like structure you want.
You don't get the total number of cards N
as an input.
Output
Print the sequence of pile sizes the game of Bulgarian Solitaire goes through. Note that printing is required, not returning. Each step should be its own line.
Each line should have a sequence of positive numbers in descending order with no 0
's. You may have separators and start and end tokens (for example, [3, 3, 1, 1]
). The numbers might have multiple digits, so they should be separated somehow.
Print the pile-size splits you see until and including reaching a repeat. So, the first line should be the input, and the last line should be a repeat of a previous line. There shouldn't be any other repeats.
Test cases
>> [1]
1
1
>> [2]
2
1 1
2
>> [1, 1, 1, 1, 1, 1, 1]
1 1 1 1 1 1 1
7
6 1
5 2
4 2 1
3 3 1
3 2 2
3 2 1 1
4 2 1
>> [5, 3]
5 3
4 2 2
3 3 1 1
4 2 2
>> [3, 2, 1]
3 2 1
3 2 1
>> [4, 4, 3, 2, 1]
4 4 3 2 1
5 3 3 2 1
5 4 2 2 1
5 4 3 1 1
5 4 3 2
4 4 3 2 1