-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.py
68 lines (54 loc) · 1.87 KB
/
day4.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
from advent import elf
def main():
test_lines = elf.lines_blank_grouped(elf.test_file(__file__))
lines = elf.lines_blank_grouped(elf.in_file(__file__))
print("Part 1 (test):")
print(part1(test_lines))
print("Part 1:")
print(part1(lines))
print("Part 2 (test):")
print(part2(test_lines))
print("Part 2:")
print(part2(lines))
def part1(lines):
to_call = list(map(int, lines[0][0].split(",")))
boards = list(map(parse_board, lines[1:]))
called = set()
for call in to_call:
called.add(call)
for board in boards:
if has_win(called, board):
print("BINGO!")
return score(call, called, board)
def parse_board(board_list):
return [elf.int_list(row.split(" ")) for row in board_list]
def has_win(called: set, board):
for row in board:
set_row = set(row)
if called.issuperset(set_row):
return True
for i in range(len(board[0])):
set_col = set([row[i] for row in board])
if called.issuperset(set_col):
return True
return False
def score(last_call, called, board):
unmarked = [n for row in board for n in row if n not in called]
total = sum(unmarked)
return total * last_call
def part2(lines):
to_call = list(map(int, lines[0][0].split(",")))
boards = list(map(parse_board, lines[1:]))
called = set()
solved_boards = set()
board_scores = [None] * len(boards)
for call in to_call:
called.add(call)
for board_num, board in enumerate(boards):
if board_num not in solved_boards and has_win(called, board):
solved_boards.add(board_num)
board_scores[board_num] = score(call, called, board)
if len(solved_boards) == len(boards):
return board_scores[board_num]
if __name__ == '__main__':
main()