Change text gravity like the 2048 puzzle In the game 2048, you have a grid, and you can move the elements in four directions. They all move in that direction as far as they can. For this challenge, you will be given a padded, square 2D string (either with newlines, or a list of strings), like so:

ab cd e 
 f ghij 
      kl
mno p   
 q r st 
u      v
 w x  y 
z       

or

['ab cd e ', ' f ghij ', '      kl', 'mno p   ', ' q r st ', 'u      v', ' w x  y ', 'z       ']


The four operations are left, right, up, and down. The result of each on the above input:

Left:

abcde   
fghij   
kl      
mnop    
qrst    
uv      
wxy     
z       

or

['abcde   ', 'fghij   ', 'kl      ', 'mnop    ', 'qrst    ', 'uv      ', 'wxy     ', 'z       ']


Right:

   abcde
   fghij
      kl
    mnop
    qrst
      uv
     wxy
       z

or

['   abcde', '   fghij', '      kl', '    mnop', '    qrst', '      uv', '     wxy', '       z']


Up:

abocdiel
mf ghsjv
un rp k 
zq x  t 
 w    y 


        

or

['abocdiel', 'mf ghsjv', 'un rp k ', 'zq x  t ', ' w    y ', '        ', '        ', '        ']


Down:

        


 b    e 
af c  j 
mn gd k 
uq rhitl
zwoxpsyv

or

['        ', '        ', '        ', ' b    e ', 'af c  j ', 'mn gd k ', 'uq rhitl', 'zwoxpsyv']




Your goal is to rotate which operation is performed each iteration, performing them on the input n times. So if your order is URDL, and the input says to start with D (2, 0-indexed), and you need 5 operations, you perform D-L-U-R-D, then print.

Input:


A string in a format like above


Trailing spaces are not required (but they are probably helpful)
It will be at least 2x2
Will only contain printable ASCII and spaces (and newlines per your input format)
You should theoretically support any length, but memory constraints are okay

A non-negative integer, n, for the number of operations that will be performed
An integer 0-3 or 1-4, or a letter UDLR, describing the operation to start with.


So your program must be able to start or end with any operation
You may define them in any order for starting purposes, but it must be a consistent order, so U cannot sometimes follow R and also sometimes follow L.

Operations must be performed non-trivially


You could do operations in the order LDRU (left, down, right, up) repeatedly, but not DLRU or UDLR (because UD is the same as D, and LR is the same just as doing R.)



Output:


The string after performing the four operations n times
The output format must be the same as your input format
Trailing spaces are not required (but they are probably helpful)


Example:

This example uses the order URDL.

Input:

10  (number of times operations are applied)
0   (starts with Up)


ab cd e 
 f ghij 
      kl
mno p   
 q r st 
u      v
 w x  y 
z       

Outputs for n = 0-5: (just print the end result)

ab cd e 
 f ghij 
      kl
mno p   
 q r st 
u      v
 w x  y 
z       
---------------
abocdiel
mf ghsjv
un rp k 
zq x  t 
 w    y 



---------------
abocdiel
 mfghsjv
   unrpk
    zqxt
      wy



---------------



      el
    dijv
   chspk
 bognrxt
amfuzqwy
---------------



el      
dijv    
chspk   
bognrxt 
amfuzqwy
---------------
eljvkxty
disprqw 
chgnz   
bofu    
am      


        

My pretty, ungolfed implementation
Jelly, 23 bytes
UZ
Ç¡=⁶$Þ€Ç$⁴¡ZU$⁵+⁴¤¡Y

Try it online!
I'm a bit unsatisfied, but MATL needed some competition. :P
Uses the order URDL. Inputs:

the input array as an array of padded lines
the number of repetitions
the move to start from (1 = U, 2 = R, 3 = D, 4 = L)

Explanation
UZ       Helper link. Argument: A (the 2D array)
U        Reverse each line and...
 Z       ...transpose. Rotates 90° CCW.

Ç¡=⁶$Þ€Ç$⁴¡ZU$⁵+⁴¤¡Y    Main link. Arguments: A, n (2D array, repetitions)
Ç                       Rotate 90° CCW...
 ¡                      ...m times. (m = which move to start on)

     Þ                  Sort...
      €                 ...each line of the array...
  =⁶                    ...based on the characters' equality to &quot; &quot;.
       Ç                Rotate 90° CCW.
        $               Combine the sort and rotate to one action.
         ⁴¡             Do that n times. (n = repetition count)

           Z            Transpose and...
            U           ...reverse each line. Rotates 90° CW.
             $          Combine the transpose and reverse to one action.
                  ¡     Do that...
              ⁵+⁴¤      ...m + n times.

                   Y    Join the array by newlines.