Card Shuffle Machine
HardOnline Assessment
Run a deck of cards through a card-shuffling machine that repeats the same program many times.
The deck has n cards labelled 0 to n - 1, and starts in that order with card 0 on top. The machine runs a program: a list of steps, each a string, applied in order to the whole deck. One run of the whole program is a pass. The steps are:
"cut k": letr = k mod n, taken as a non-negative remainder (so-1behaves liken - 1, andn + 2like2). Move the toprcards to the bottom, keeping their order. Equivalently, a negativekmoves the bottom|k| mod ncards to the top, keeping their order."flip m": reverse the order of the topmin(m, n)cards. The rest of the deck is untouched."weave": split the deck into a top half of the firstceil(n / 2)cards and a bottom half of the rest (so with an odd number of cards the top half is one larger). Then interleave them, starting with the top half's first card: top 1st, bottom 1st, top 2nd, bottom 2nd, and so on. A deck of 1 card is unchanged."deal s": deal the deck from the top, one card at a time, intospiles numbered 0 tos - 1: the card at positioni(counting from 0) goes face-up on top of pilei mod s. Then pick the piles up one after another, pile 0 first, each read from its top card to its bottom card, and that sequence is the new deck from the top down. (Ifs > nthe extra piles are empty. Note that each pile is the reverse of the order its cards were dealt in;deal 1therefore reverses the whole deck.)
The program is repeated times times, one pass after another, on the same deck. The number of passes is huge, so you cannot simulate them one by one.
Input: n, the number of cards; program, a list of step strings; times, the number of passes.
Output: the deck after all passes, as a list of card labels from the top card to the bottom card. With times = 0 or an empty program the deck is unchanged.
Example 1
- Input:
- [7,["cut 2","weave"],1]
- Output:
- [2,6,3,0,4,1,5]
- Explanation
- The deck starts as 0..6.
cut 2moves the top two cards to the bottom: 2 3 4 5 6 0 1.weavesplits it into the top 4 cards (2 3 4 5) and the bottom 3 (6 0 1) and interleaves them starting from the top half: 2 6 3 0 4 1 5.
Example 2
- Input:
- [6,["deal 2","flip 3"],1]
- Output:
- [0,2,4,5,3,1]
- Explanation
deal 2puts cards 0..5 alternately on two piles, each new card on top of its pile, so pile 0 reads 4 2 0 from the top and pile 1 reads 5 3 1; picking up pile 0 then pile 1 gives 4 2 0 5 3 1.flip 3reverses the top three cards: 0 2 4 5 3 1.
Example 3
- Input:
- [5,["cut 1","flip 3"],1000000000000]
- Output:
- [3,1,2,4,0]
- Explanation
- One pass turns 0 1 2 3 4 into 3 2 1 4 0: positions 0, 3 and 4 form a cycle of length 3 (position 0 receives the card from 3, position 3 from 4, position 4 from 0), and positions 1 and 2 swap. 1012 passes is 1 modulo 3 along the 3-cycle (a net single step) and 0 modulo 2 along the swap (nothing), giving 3 1 2 4 0.
Constraints
- 1 ≤ n ≤ 1000
- 0 ≤ program.length ≤ 40; each step is
cut k(-109 ≤ k ≤ 109),flip m(0 ≤ m ≤ 109),weave, ordeal s(1 ≤ s ≤ 109) - 0 ≤ times ≤ 1015