-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem-1.py
56 lines (49 loc) · 1.9 KB
/
Problem-1.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
class BingoBoard:
def __init__(self, board:list[list[int]], numbers: list[int]):
self.board = board
self.numbers = numbers
@property
def win(self) -> bool:
for row in self.board:
if all( (i in self.numbers) for i in row ):
return True
for row_i in range(len(self.board)):
if all( (self.board[col_i][row_i] in self.numbers) for col_i in range(len(self.board[row_i])) ):
return True
return False
@property
def unused_numbers(self) -> list[int]:
return [ num for row in self.board for num in row if num not in self.numbers ]
def preprocess(data: str, numbers: str) -> tuple[list[BingoBoard], list[int]]:
raw_data: list[str] = data.split('\n')
raw_numbers = [int(num) for num in raw_data.pop(0).split(',')]
raw_data.pop(0)
boards = []
board_data = []
for line in raw_data:
if line == '':
continue
board_data.append([int(number) for number in line.split()])
if len(board_data) == 5:
boards.append(BingoBoard(board_data, numbers))
board_data = []
return boards, raw_numbers
def process(boards: list[BingoBoard], numbers: list[int], unused_numbers: list[int]) -> int:
win_board = None
win_number = None
for num in unused_numbers:
numbers.append(num)
if any(board.win for board in boards):
win_number = num
win_board = (board for board in boards if board.win).send(None)
break
print('Winning Number:', win_number)
print('Sum of Unused Numbers:', s:=sum(win_board.unused_numbers))
print('Result:', result:=s*win_number)
return result
if __name__ == '__main__':
numbers = []
with open('.\\Input-1.txt', 'r') as f:
inp = f.read()
boards, unused_numbers = preprocess(inp, numbers)
process(boards, numbers, unused_numbers)