Resolve doubling and tripling actions in Dominion Inspiration

This question is inspired by the cards Throne Room and King's Court from the popular deck-building card game Dominion. 

 

As part of one's turn, one plays a sequence of actions. These two particular actions cause the next-played action to repeat two or three times*. Other "generic" actions cause specific game effects, but we won't be interested in the particulars, simply labelling them with letters. 

The interesting case is when a Throne Room or King's Court affects another Throne Room of King's Court, causing the doubling or tripling effect to itself be doubled or tripled. Long chains of Throne Rooms, King Courts, and multiplied actions can confuse even experienced Dominion players. 

Your goal is to write code that correctly resolves these chains, using as few bytes as possible. I'll describe the program requirements before explaining how the chains resolve in the Dominion rules.

*Technically, you choose the action affected as part of resolving Throne Room or King's Court, but this view is cleaner for this challenge.

Program Requirements

Write a program or named function. It should take in the chain of actions played (STDIN or function input), and output or print the resulting chain of actions from the effects of doubling and tripling. Fewest bytes wins.

Input

A string representing the sequence of actions played. Generic actions are represented by capital letters A through Z. The special doubling action Throne Room is represented by the character 2, and the tripling action King's Court by 3,

The number of characters (actions) will be between 1 and 30, inclusive. You may have the input end in a newline if you wish.

Example input: WA23G3GA 

Output

A string of capital lettersA to Z. This should be the sequence of generic actions that results from resolving the doubling and tripling effects, in the order than they happen.

You may have the output end in a newline if you wish. There should be no additional characters otherwise.

Example output: WAGGGGGGAAA.

How doubling and tripling works in Dominion

Here, I'll go through how chains of Throne Rooms (2's) and King's Courts (3's) work as per the Dominion rules.

After you play a 2, the next action to be resolved happens twice. So, if you first play 2, then A, you get A happening twice. 

2A -&gt; AA


Similarly, 

A2BC -&gt; ABBC
3DE -&gt; DDDE
3N2BC3XY2 -&gt; NNNBBCXXXY


Note in the last example that the final 2 had nothing to double, so it had no effect.

The interesting thing happens when doubling or tripling effects are themselves doubled or tripled. For example,

22AB -&gt; AABB


First, you play 2. Then, you play another 2, which is doubled from the previous 2. As a result, the next two actions are doubled. First, the two copies of A resolve. Then, the copies of B resolve. 

Note that A is not quadrupled: after the first copy of 2 acts on the first A, the next copy acts on the next unresolved action, which is B. Without the B, we'd have

22A -&gt; AA


where the second copy of 2 is waiting for the next action to double, but no action comes.

Finally, let's look at a complex example. 

223BCDE -&gt; BBBCCCDDE


As before, the first 2 causes the second 2 to be doubled. So, the next two actions will be doubled. The first copy of 2 doubles the next action 3, which must be resolved completely before resolving the next copy of 2. The first copy of 3 triples B, and the second copy triples C. Now, the still-waiting second copy of 2 doubles the next still-unresolved action, which is D. After this, no doubling or tripling effects remain, and the final action E simply happens.

Test cases

These are given as (input,output).

(FY, FY)
(A2BC, ABBC)
(3DE, DDDE)
(3N2BC3XY2, NNNBBCXXXY)
(WA23G3GA, WAGGGGGGAAA)
(32, )
(33RST, RRRSSSTTT)
(2A32B2CDEFG, AABBCCDDEEFG)
(A2A323AB2CD2D2E3ABC, AAAAAABBBCCDDDDEEAAABBBC)
(P22LL3Q2Q22T, PLLLLQQQQQTT)
(322322ABCDEFGHIJKLMN, AABBCCDDEEEFFGGHHIJKLMN)
GolfScript (29 26 bytes)

](1/{\1+(3&amp;@*.23-\1$-@+}/;


Online demo

Dissection

This slightly abuses the loose typing of GolfScript. The stack of how many times to repeat subsequent actions starts out as an array and later turns into a string - but 1+ appends a 1 and (3&amp; pops the first value and correctly puts it in the range 0 to 3 regardless of the type change.

](         # Push an empty array under the input string to serve as rep stack
1/{        # Loop over the input string as a series of 1-char strings
           #   Stack is ... reps ch
           #   where the ... covers zero or more strings which will be output
  \        #   Bring the rep stack to the top
  1+(      #   Push a `1` on the bottom of it to avoid underflow and then pop
  3&amp;       #   Coerce to correct range, because if rep stack is a string then
           #   we just got an ASCII value
  @*       #   Apply repetition to the 1-char string: it's now an n-char string
  .23-     #   Duplicate it and remove chars '2' and '3': this becomes output
  \1$-     #   Get the original copy and remove the output string's chars
           #   So the stack is now ... reps output non-output
           #   where non-output is either an empty string or a string of '2's
           #   or '3's
  @+       #   Push non-output onto the repetition stack
}/         # Loop
;          # Pop whatever's left of the repetition stack