-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart2.py
More file actions
41 lines (30 loc) · 1.04 KB
/
part2.py
File metadata and controls
41 lines (30 loc) · 1.04 KB
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
from copy import deepcopy
with open("input.txt") as file:
sequence = list(map(int, file.readline().split(',')))
boards = [[]]
for line in filter(lambda line: line.strip(), file):
line = list(map(int, line.split()))
if len(boards[-1]) == 5:
boards.append([line])
else:
boards[-1].append(line)
has_won = [False] * len(boards)
for number in sequence:
for board_index, board in enumerate(boards):
for line in board:
for i in range(5):
if line[i] == number:
line[i] = None
for i in range(5):
if (
all(board[i][j] is None for j in range(5))
or all(board[j][i] is None for j in range(5))
) and not has_won[board_index]:
has_won[board_index] = True
last_won = deepcopy(board)
last_won_number = number
board_sum = sum(
sum(filter(lambda item: item is not None, line))
for line in last_won
)
print(board_sum * last_won_number)