This question evaluates proficiency in implementing deterministic game-state logic and compact binary representations, specifically array manipulation and merge semantics for 2048 slide operations plus bit-level packing/unpacking of 4-bit exponent fields.
You are implementing part of the 2048 game on a fixed 4×4 grid.
Given a 4×4 board of non-negative integers (each is either 0 or a power of two), implement a function:
move(board, dir) -> new_board
where dir ∈ {LEFT, RIGHT, UP, DOWN}.
Rules (standard 2048):
2x
.
Implement two functions:
encode(board) -> uint64
decode(x) -> board
Constraints/assumptions for encoding:
e
where value =
2^e
.
e = 0
.
2^15
(so exponent fits in 4 bits).
decode(encode(board))
must reproduce the original board.
Describe/implement move, encode, and decode with correct handling of merges and bit packing.