-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.py
173 lines (145 loc) · 4.45 KB
/
4.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from utils import get_input
source = [val for val in get_input(day=4).split("\n")]
# Sample source
# source = [
# "7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1",
# "",
# "22 13 17 11 0",
# " 8 2 23 4 24",
# "21 9 14 16 7",
# " 6 10 3 18 5",
# " 1 12 20 15 19",
# "",
# " 3 15 0 2 22",
# " 9 18 13 17 5",
# "19 8 7 25 23",
# "20 11 10 24 4",
# "14 21 16 12 6",
# "",
# "14 21 17 24 4",
# "10 16 15 9 19",
# "18 8 23 26 20",
# "22 11 13 6 5",
# " 2 0 12 3 7",
# ]
class Board:
def __init__(self, index, board_matrix):
self.board = {}
self.isDone = False
self.name = f"Board {index}"
self.init_board(board_matrix)
def init_board(self, matrix):
"""
'92 3 88 13 50',
'90 70 24 28 52',
'15 98 10 26 5',
'84 34 37 73 87',
'25 36 74 33 63',
"""
for row_number, row in enumerate(matrix):
numbers = [e for e in row.split(" ") if e != ""]
for col_number, number in enumerate(numbers):
# 92: (0, 0, False) = (row, col, visited)
self.board[number] = (row_number, col_number, {"marked": False})
def mark_number(self, value):
"""
When a number is called out, if the value is in our board
mark it.
"""
if value in self.board:
self.board[value][2]["marked"] = True
def sum_of_unmarked(self):
return sum(
int(number)
for number, entry in self.board.items()
if entry[2]["marked"] == False
)
def check_rows(self):
"""
Based on the defition of the board model, each individual entry
in the board has a (row, col, {marked: bool}). To check the rows
check the values in position 0.
"""
return self.check_index(0)
def check_cols(self):
"""
Based on the defition of the board model, each individual entry
in the board has a (row, col, {marked: bool}). To check the rows
check the values in position 1.
"""
return self.check_index(1)
def check_index(self, index):
"""
If all the entries in a given row or column are set as marked,
return the index of that given position. The caller would know if
the index represents a row or a column based on the index passed in.
index: 0 => row
index: 1 => col
"""
for position in range(5):
if all(
entry[2]["marked"]
for entry in self.board.values()
if entry[index] == position
):
return position
return None
def print_row(self, row_num):
result = " ".join(
[
f"{number}{str(entry[2]['marked'])[0]}"
for number, entry in self.board.items()
if entry[0] == row_num
]
)
print(result)
def bingo(self):
result = self.check_rows() is not None or self.check_cols() is not None
if result:
self.isDone = True
return result
def move(self, number):
self.mark_number(number)
return not self.isDone and self.bingo()
def print(self):
for row in range(5):
self.print_row(row)
def build_boards(source):
boards = []
rows = []
for record in source:
if record == "":
boards.append(Board(len(boards) + 1, rows))
rows = []
continue
rows.append(record)
if len(rows):
# Append the last board, given that the source might not have
# a final "" separator
boards.append(Board(len(boards) + 1, rows))
return boards
bingo_numbers = source.pop(0).split(",")
source.pop(0) # consume ''
boards = build_boards(source)
print("PART 1")
def move(number, game_boards):
for board in game_boards:
board.mark_number(number)
if board.bingo():
board.print()
print(f"{board.name} won with number {number}")
print(int(number) * board.sum_of_unmarked())
return True
return False
for number in bingo_numbers:
if move(number, boards):
break
print()
print("PART 2")
score_board = [
int(number) * board.sum_of_unmarked()
for number in bingo_numbers
for board in boards
if board.move(number)
]
print(score_board[-1])