-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
class Bingo: | ||
def __init__(self, board): | ||
self.board = [[int(n) for n in row.split()] for row in board.split("\n")] | ||
self.skip = False | ||
|
||
def winner(self): | ||
for row in self.board: | ||
if all(n == -1 for n in row): | ||
self.skip = True | ||
return True | ||
for i in range(5): | ||
column = [self.board[j][i] for j in range(5)] | ||
if all(n == -1 for n in column): | ||
self.skip = True | ||
return True | ||
return False | ||
|
||
def draw(self, n): | ||
for r, row in enumerate(self.board): | ||
for c, num in enumerate(row): | ||
if num == n: | ||
self.board[r][c] = -1 | ||
|
||
def unmarked_sum(self): | ||
ans = 0 | ||
for row in self.board: | ||
ans += sum(x for x in row if x != -1) | ||
return ans | ||
|
||
|
||
f = open('input04.txt') | ||
numbers = map(int, next(f).strip().split(',')) | ||
next(f) | ||
boards = [Bingo(b) for b in f.read().strip().split("\n\n")] | ||
|
||
def main(): | ||
for n in numbers: | ||
for b in boards: | ||
b.draw(n) | ||
if b.winner(): | ||
return b.unmarked_sum() * n | ||
|
||
if __name__ == '__main__': | ||
print(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from day04a import numbers, boards | ||
|
||
def main(): | ||
last_score = 0 | ||
for n in numbers: | ||
for b in boards: | ||
# skip if the Bingo board already won | ||
if b.skip: | ||
continue | ||
b.draw(n) | ||
if b.winner(): | ||
last_score = n * b.unmarked_sum() | ||
print(last_score) | ||
return last_score | ||
|
||
if __name__ == '__main__': | ||
print(main()) |