"""Play bingo""" import sys class BingoBoard(): def __init__(self, lines): self.board = [] self.called = [ [False] * 5, [False] * 5, [False] * 5, [False] * 5, [False] * 5, ] for line in lines: self.board.append([int(n) for n in line.strip().split()]) def number_called(self, number): for i, row in enumerate(self.board): for j, n in enumerate(row): if n == number: self.called[i][j] = True return self.won() return False def won(self): for i, row in enumerate(self.called): if row == [True] * 5: return True cols_called = list(zip(*self.called)) cols_board = list(zip(*self.board)) for j, col in enumerate(cols_called): if col == tuple([True] * 5): return True return False def get_score(self): score = 0 for i in range(5): for j in range(5): if not self.called[i][j]: score += self.board[i][j] print(self.board[i][j]) return score bingo_boards = [] with open("4_input.txt") as f: bingo_calls = [int(n) for n in f.readline().split(",")] lines = f.readlines() for i in range(0, len(lines), 6): bingo_boards.append(BingoBoard(lines[i+1:i+6])) for call in bingo_calls: for board in bingo_boards: won = board.number_called(call) if won: score = board.get_score() print(score) print(call) print(score * call) sys.exit(0)