-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday4-1.py
67 lines (46 loc) · 1.97 KB
/
day4-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
57
58
59
60
61
62
63
64
65
66
67
from typing import Dict, List, Tuple
lines = [line.strip() for line in open('2021//input//day4.txt', 'r').readlines()]
def get_boards(lines: List[str]) -> List[List[List[int]]]:
boards = []
current_board = []
for line in lines[1:]:
if line == "":
if current_board:
boards.append(current_board)
current_board = []
else:
current_board.append([int(number) for number in line.split(' ') if number != ''])
if current_board:
boards.append(current_board)
return boards
def get_numbers(lines: List[str]) -> Dict[int, int]:
numbers: List[int] = [int(number) for number in lines[0].split(',')]
call_order: Dict[int, int] = {}
for i, number in enumerate(numbers):
call_order[number] = i
return call_order
def calculate_best_board(boards, call_order) -> Tuple[int, int]:
wins_at = len(call_order.keys())
winning_board = -1
for board_i, board in enumerate(boards):
min_board_call = len(call_order.keys())
# rows
for row in board:
max_row_call = max(call_order[number] for number in row)
min_board_call = min(min_board_call, max_row_call)
# columns
for column in zip(*board):
max_column_call = max(call_order[number] for number in column)
min_board_call = min(min_board_call, max_column_call)
if min_board_call < wins_at:
wins_at = min_board_call
winning_board = board_i
return wins_at, winning_board
def calculate_board(board, wins_at, call_order):
num_called = list(call_order.keys())[list(call_order.values()).index(wins_at)]
sum_not_called = sum(number for row in board for number in row if call_order[number] > wins_at)
return num_called * sum_not_called
boards = get_boards(lines)
call_order = get_numbers(lines)
wins_at, best_board = calculate_best_board(boards, call_order)
print(calculate_board(boards[best_board], wins_at, call_order))