This question evaluates a candidate's ability to implement array and matrix manipulation, deterministic state-transition logic, and rule-based merging behavior in a grid-based simulation.
You are given a 4×4 board for the game 2048. Each cell contains either 0 (empty) or a power of two (2, 4, 8, ...). When the player swipes in one of four directions (up, down, left, right), the board updates as follows:
Implement a function that applies exactly one swipe (tilt) to the board.
board
: 4×4 integer matrix
dir
: one of
{up, down, left, right}
[2, 0, 2, 4]
→
[4, 4, 0, 0]
[2, 2, 2, 0]
→
[4, 2, 0, 0]
(not
[8, 0, 0, 0]
)
[4, 4, 4, 4]
→
[8, 8, 0, 0]