from utils import open_day nums, *boards = open_day(4).read().split('\n\n') nums = list(map(int, nums.split(','))) boards = [[[(int(n), False) for n in l.split()] for l in b.rstrip().split('\n')] for b in boards] def win(board): for y in board: if all(x[1] for x in y): return True for x in range(len(board[0])): if all(board[y][x][1] for y in range(len(board))): return True return False won = set() wins = [] for n in nums: for i, b in enumerate(boards): tot = 0 for y in range(len(b)): for x in range(len(b[y])): if b[y][x][0] == n: b[y][x] = (b[y][x][0], True) if b[y][x][1] == False: tot += b[y][x][0] if i not in won and win(b): won.add(i) wins.append(n * tot) print(wins[0]) print(wins[-1])