blob: 0f25ad03beeb2c7463547a167923b5b5eb365a93 (
plain)
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
|
import sys
data = sys.argv[1] if len(sys.argv) > 1 else "../day4data.txt"
numbers = None
B = []
F = []
board = []
for line in open(data):
line = line.strip()
if numbers is None:
numbers = [int(x) for x in line.split(",")]
else:
if line:
board.append([int(x) for x in line.split()])
else:
if board:
B.append(board)
board = []
B.append(board)
for b in B:
assert len(b) == 5 and len(b[0]) == 5
F.append([[False for _ in range(5)] for _ in range(5)])
WON = [False for _ in range(len(B))]
for num in numbers:
for i in range(len(B)):
for r in range(5):
for c in range(5):
if B[i][r][c] == num:
F[i][r][c] = True
won = False
for r in range(5):
ok = True
for c in range(5):
if not F[i][r][c]:
ok = False
if ok:
won = True
for c in range(5):
ok = True
for r in range(5):
if not F[i][r][c]:
ok = False
if ok:
won = True
if won and not WON[i]:
WON[i] = True
nwin = len([j for j in range(len(B)) if WON[j]])
if nwin == 1 or nwin == len(B):
unmarked = 0
for r in range(5):
for c in range(5):
if not F[i][r][c]:
unmarked += B[i][r][c]
print(unmarked * num)
|