-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.py
72 lines (63 loc) · 2.1 KB
/
day04.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
from utils import get_input
def part1(numbers, boards):
for i in range(len(numbers)):
drawn = numbers[:i]
winner_board = get_winner(numbers[:i], boards)
if winner_board:
return get_score(drawn, winner_board)
def get_winner(drawn, boards):
found_winner = False
for board in boards:
board_transposed = list(map(list, zip(*board)))
for b in [board, board_transposed]:
for line in b:
found_in_line = True
for number in line:
if number not in drawn:
found_in_line = False
break
if found_in_line:
return board
return None
def get_score(drawn, board):
s = 0
for line in board:
for number in line:
if number not in drawn:
s += int(number)
return s * int(drawn[-1])
def get_winners(drawn, boards):
found_winner = False
winners = []
for board in boards:
board_transposed = list(map(list, zip(*board)))
for b in [board, board_transposed]:
for line in b:
found_in_line = True
for number in line:
if number not in drawn:
found_in_line = False
break
if found_in_line:
if board not in winners:
winners.append(board)
return winners
def part2(numbers, boards):
for i in range(len(numbers)):
drawn = numbers[:i]
for winner_board in get_winners(numbers[:i], boards):
boards.remove(winner_board)
if len(boards) == 0:
return get_score(drawn, winner_board)
def process_data(data):
numbers = data[0].split(",")
boards = []
for i in range(round(len(data[2:])/6)):
boards.append([x.split() for x in data[2+6*i:7+6*i]])
return numbers, boards
if __name__ == "__main__":
day = 4
data = get_input(day)
numbers, boards = process_data(data)
print(part1(numbers, boards))
print(part2(numbers, boards))