Level 1 Sliding the tiles
A 2048 board is a 4x4 grid given as a list of 4 rows, each a list of 4 ints. A cell holds 0 (empty) or a power of two >= 2.
Write move(board, direction) -> list[list[int]] where direction is one of "left", "right", "up", "down". Return a new board (do not modify the input) after one move:
- Every tile slides as far as it can toward the chosen side.
- Two equal tiles that meet combine into one tile of twice the value. Pairing happens starting from the side you move toward.
- A tile produced by a combination cannot combine again in the same move. So
[4, 4, 8, 0]moved left gives[8, 8, 0, 0], not[16, 0, 0, 0]. - Empty cells end up on the side opposite the move.
No new random tile is added; that's the game's job, not yours.
move([[2, 2, 2, 2],
[0, 4, 0, 4],
[8, 0, 8, 8],
[2, 0, 0, 0]], "left")
# [[4, 4, 0, 0],
# [8, 0, 0, 0],
# [16, 8, 0, 0],
# [2, 0, 0, 0]]
move([[2, 0, 0, 0],
[2, 0, 0, 0],
[4, 0, 0, 0],
[0, 0, 0, 0]], "up")
# first column becomes [4, 4, 0, 0]
Tip: write the logic once for a single row moving left, then get the other three directions by reversing rows and/or transposing.