2048 Move Simulator
Problem statement
Apply every move in moves to the square 2048 grid. A move slides nonzero tiles toward its edge and merges each adjacent equal pair once. Do not spawn new tiles. Return the final grid.
Function
simulate2048(grid: int[][], moves: String) → int[][]Examples
Example 1
grid = [[2,0,2,2],[0,0,0,0],[0,0,0,0],[0,0,0,0]]moves = "L"return = [[4,2,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]The first pair merges once and the third tile remains.
Constraints
2 <= grid.length <= 20and the grid is square.- Every entry is zero or a positive power of two.
movescontains onlyL,R,U, andD.- For this exercise, assume the sum of all initial tile values is at most
2147483647. Moves conserve this sum because no tiles spawn.