-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4-2.py
81 lines (61 loc) · 2.09 KB
/
day4-2.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
73
74
75
76
77
78
79
80
81
from typing import List, Tuple
import re
class Board:
def __init__(self, content: List[List[int]]):
self.content = content
self.marked = [[False for _ in row] for row in content]
self.is_still_playing = True
def mark(self, value: int) -> None:
for i in range(len(self.content)):
for j in range(len(self.content[i])):
if self.content[i][j] == value:
self.marked[i][j] = True
def has_won(self) -> bool:
for row in self.marked:
if all(row):
return True
for col in zip(*self.marked):
if all(col):
return True
return False
def get_sum_of_unmarked_values(self) -> int:
sum = 0
for i, row in enumerate(self.marked):
for j, value in enumerate(row):
if not value:
sum += self.content[i][j]
return sum
def parse_input() -> Tuple[List[int], List[Board]]:
boards: List[Board] = []
board_content: List[List[int]] = []
draws: List[int]
with open("day4-1-input.txt") as lines:
for i, line in enumerate(lines):
if i <= 1:
draws = [int(c) for c in line.strip().split(",")] if i == 0 else draws
continue
if line == "\n":
boards.append(Board(board_content))
board_content = []
else:
board_content.append(
[int(c) for c in re.sub(" +", " ", line.strip()).split(" ")]
)
boards.append(Board(board_content))
return draws, boards
def main():
draws, boards = parse_input()
place = 0
for draw in draws:
for board in boards:
if not board.is_still_playing:
continue
board.mark(draw)
if board.has_won():
board.is_still_playing = False
place += 1
if place == len(boards):
print(board.get_sum_of_unmarked_values() * draw)
return
if __name__ == "__main__":
main()