-
Notifications
You must be signed in to change notification settings - Fork 1
/
2048.py
72 lines (64 loc) · 2.05 KB
/
2048.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from sys import stdin
from collections import deque
from bisect import bisect_right
input = stdin.readline
def parityX():
zeroIndex = set()
for j in range(4):
if grid[0][j] == 0:
zeroIndex.add(j)
for i in grid[1:]:
for j in range(4):
if j in zeroIndex and i[j] != 0:
return False
elif j not in zeroIndex and i[j] == 0:
return False
return testNumbers()
def parityY():
for i in grid:
zeroCount = 0
for j in i:
if j == 0:
zeroCount += 1
if zeroCount == 0 or zeroCount == 4:
pass
else:
return False
return testNumbers()
def testNumbers():
global moves
visited = [[False] * 4 for _ in range(4)]
for i in range(4):
for j in range(4):
if not visited[i][j]:
visited[i][j] = True
queue = deque()
queue.append([i, j])
while queue:
a, b = queue.popleft()
for m, n in moves:
x = a + m
y = b + n
while 0 <= x < 4 and 0 <= y < 4 and grid[x][y] == 0:
x += m
y += n
if 0 <= x < 4 and 0 <= y < 4 and not visited[x][y]:
if grid[x][y] != 0 and grid[x][y] == grid[a][b]:
return False
visited[x][y] = True
queue.append([x, y])
return True
moves = [[1, 0], [0, 1], [-1, 0], [0, -1]]
floorList = [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048]
for _ in range(5):
grid = [[int(x) for x in input().split()] for _ in range(4)]
if parityX() or parityY():
maximum = 0
for i in grid:
maximum = max(maximum, max(i))
else:
maximum = 0
for i in grid:
maximum += sum(i)
maximum = floorList[bisect_right(floorList, maximum) - 1]
print(maximum)