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-3or1-4, or a letterUDLR, 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
Ucannot sometimes followRand also sometimes followL.
- Operations must be performed non-trivially
- You could do operations in the order
LDRU(left, down, right, up) repeatedly, but notDLRUorUDLR(becauseUDis the same asD, andLRis the same just as doingR.)
- You could do operations in the order
Output:
- The string after performing the four operations
ntimes - 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