Skip to content

Commit

Permalink
modified day 4 and .gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
djsheehy committed Dec 5, 2021
1 parent 8bb991a commit 13ef011
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
44 changes: 44 additions & 0 deletions 2021/day04a.py
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())
17 changes: 17 additions & 0 deletions 2021/day04b.py
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())

0 comments on commit 13ef011

Please sign in to comment.