Apply a 2048 Board Move
Problem statement
You are given a fixed 4 x 4 2048 board and one direction: "LEFT", "RIGHT", "UP", or "DOWN". Apply one move and return the resulting board.
For every row or column, remove zeros toward the chosen edge, merge equal adjacent tiles once per move from that edge outward, and compact again. A tile created by a merge cannot merge again during the same move. Do not spawn a random tile.
Function
move2048(board: int[][], direction: String) → int[][]Examples
Example 1
board = [[2,0,2,2],[4,4,4,4],[0,0,2,2],[2,2,2,0]]direction = "LEFT"return = [[4,2,0,0],[8,8,0,0],[4,0,0,0],[4,2,0,0]]Each row compacts left and each original tile participates in at most one merge.
Example 2
board = [[2,0,0,2],[2,0,0,2],[4,0,0,4],[0,0,0,0]]direction = "UP"return = [[4,0,0,4],[4,0,0,4],[0,0,0,0],[0,0,0,0]]The two columns are processed independently toward the top edge.
Constraints
board.length == 4and every row has length4.- Every cell is zero or a positive power of two no greater than
1073741824. directionis one ofLEFT,RIGHT,UP, orDOWN.- Every merged value fits signed 32-bit arithmetic.