67 lines
2.1 KiB
Python
67 lines
2.1 KiB
Python
|
#!/usr/bin/env python3
|
||
|
|
||
|
class BingoBoard():
|
||
|
def __init__(self, board):
|
||
|
self._hits = [[False for x in range(5)] for x in range(5)]
|
||
|
self._board = board
|
||
|
self._remaining = 0
|
||
|
self.bingo = False
|
||
|
for line in board:
|
||
|
line = list(map(int, line))
|
||
|
self._remaining += sum(line)
|
||
|
|
||
|
def isBingo(self):
|
||
|
rowBingo = any(all(row) for row in self._hits)
|
||
|
colBingo = any(all(row[col] for row in self._hits) for col in range(len(self._board)))
|
||
|
if colBingo or rowBingo:
|
||
|
self.bingo = True
|
||
|
return self.bingo
|
||
|
|
||
|
def checkField(self, field):
|
||
|
for row_idx, row in enumerate(self._board):
|
||
|
try:
|
||
|
col_idx = row.index(field)
|
||
|
self._hits[row_idx][col_idx] = True
|
||
|
self._remaining -= int(field)
|
||
|
return True
|
||
|
except ValueError:
|
||
|
pass
|
||
|
return False
|
||
|
def getRemaining(self):
|
||
|
return self._remaining
|
||
|
|
||
|
def chunker(seq, size):
|
||
|
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
|
||
|
|
||
|
def main():
|
||
|
content = open('input', 'r').readlines()
|
||
|
content = list(map(str.strip, content))
|
||
|
content = [line for line in content if line != ""]
|
||
|
draws = content.pop(0).split(',')
|
||
|
print(draws)
|
||
|
boards = []
|
||
|
for board in chunker(content, 5):
|
||
|
board = list(map(str.split, board))
|
||
|
boards.append(BingoBoard(board))
|
||
|
print(f'Loaded {len(boards)} boards, playing {len(draws)} moves')
|
||
|
boards_finished = 0
|
||
|
total_boards = len(boards)
|
||
|
for draw in draws:
|
||
|
print(f'New number drawn: {draw}')
|
||
|
boards_hit = 0
|
||
|
for board in boards:
|
||
|
if not board.bingo:
|
||
|
if board.checkField(draw):
|
||
|
boards_hit += 1
|
||
|
if board.isBingo():
|
||
|
print('WE HAVE A WINNER!')
|
||
|
print(board.getRemaining() * int(draw))
|
||
|
boards_finished += 1
|
||
|
#boards.remove(board)
|
||
|
#break
|
||
|
print(f'{boards_hit} hit this draw')
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|