-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2021_4_squid.py
50 lines (41 loc) · 1.48 KB
/
2021_4_squid.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
f = open("2021_4_squid_input.txt")
class Board:
def __init__(self, rows):
self.unmarked_row_sets = [set(row) for row in rows]
self.unmarked_column_sets = [set(column_tuple)
for column_tuple in zip(*rows)]
self.num_balls_added = 0
def add_ball(self, ball):
self.num_balls_added += 1
bingo = False
for line in self.unmarked_row_sets + self.unmarked_column_sets:
line.discard(ball)
if not line:
bingo = True
return bingo
def get_total_unmarked_numbers(self):
return sum(sum(int(num) for num in unmarked_row_set) for unmarked_row_set in self.unmarked_row_sets)
lines = (line.strip() for line in f)
balls = next(lines).split(',')
print(balls)
current_rows = []
worst_board = None
for line in lines:
print(line)
if line:
current_rows.append(line.split())
else:
if not current_rows:
continue
current_board = Board(current_rows)
for ball in balls:
bingo = current_board.add_ball(ball)
if bingo:
break
if worst_board is None or current_board.num_balls_added > worst_board.num_balls_added:
worst_board = current_board
current_rows = []
print(worst_board.get_total_unmarked_numbers())
print(int(balls[worst_board.num_balls_added - 1]))
print(worst_board.get_total_unmarked_numbers() *
int(balls[worst_board.num_balls_added - 1]))