blob: 581aef550b867951cf58a39e9435647e9909275b (
plain) (
blame)
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
|
#!/usr/bin/env python3
board = list[list[int]]
last: board = [[]] * 5
def bingo(board: board) -> bool:
global last
if any(
board[0][i] == board[1][i]
and board[1][i] == board[2][i]
and board[2][i] == board[3][i]
and board[3][i] == board[4][i]
or len(set(board[i])) == 1
for i in range(5)
):
last = board
return True
return False
def main() -> None:
boards: list[board] = []
with open("input", "r", encoding="utf-8") as f:
lines = f.readlines()
for i, line in enumerate(lines):
if i == 0:
draws = list(map(int, line.strip().split(",")))
elif line == "\n":
boards.append([list(map(int, lines[i + j].strip().split())) for j in range(1, 6)])
# START PART 1
while not any(bingo(b) for b in boards):
# END PART 1 START PART 2
while len(draws) > 0 and len(boards) > 0:
# END PART 2
num = draws.pop(0)
for i, board in enumerate(boards):
for j, row in enumerate(board):
boards[i][j] = [-1 if n == num else n for n in row]
# START PART 2
boards = list(filter(lambda b: not bingo(b), boards))
# END PART 2
print(num * sum(map(lambda x: sum(filter(lambda n: n != -1, x)), last)))
if __name__ == "__main__":
main()
|