FastPrep2048 Move Simulator

2048 Move Simulator

Benchling logoBenchling● MediumNEW GRADOA
Learn

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 <= 20 and the grid is square.
  • Every entry is zero or a positive power of two.
  • moves contains only L, R, U, and D.
  • For this exercise, assume the sum of all initial tile values is at most 2147483647. Moves conserve this sum because no tiles spawn.

More Benchling problems

See Benchling hiring insights
public int[][] simulate2048(int[][] grid, String moves) {
    // Write your code here.
}
grid[[2,0,2,2],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
moves"L"
expected[[4,2,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
Checking account…