Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
9 lines (2nd try) solution in Creative category for Behind 2048 by CDG.Axel
def line_left(line):
return __import__('functools').reduce(lambda a, b: a[:b] + [2*a[b]] + a[b+2:] + [0] if a[b] == a[b+1] else a,
range(3), list(filter(None, line)) + [0] * line.count(0))
def move2048(state, move):
rotate, stp = (lambda x: list(map(list, zip(*x)))) if move in 'up down' else list, (-1) ** (move in 'right down')
state = rotate(line_left(i[::stp])[::stp] for i in rotate(state))
if (win := any(2048 in i for i in state)) or not any(0 in i for i in state):
return [list('U'+'WIN')]*4 if win else [list('GAME'), list('OVER')]*2
return [[2 if i*4+j == (15-sum(state, [])[::-1].index(0)) else state[i][j] for j in range(4)] for i in range(4)]
Nov. 13, 2021
Comments: