<gh_stars>0
with open('input.txt', 'r') as f:
    lines = f.read().splitlines()

nums = list(map(int, lines[0].split(',')))
lines = lines[2:]

boards = [
    [list(map(int, filter(None, row.split()))) for row in lines[i:i + 5]]
    for i in range(0, len(lines), 6)
]


found = False
for i in range(1, len(nums)):
    n = nums[:i]
    for board in boards.copy():
        for b in (board, zip(*board)):
            b = [[x for x in row if x not in n] for row in b]
            if not all(b):  # if empty row found
                # part 1
                if not found:
                    print(sum(map(sum, b)) * nums[i - 1])
                    found = True
                # part 2
                if len(boards) == 1:
                    print(sum(map(sum, b)) * nums[i - 1])
                boards.remove(board)
                break

with open('input.txt', 'r') as f:
    lines = f.read().splitlines()

nums = list(map(int, lines[0].split(',')))
lines = lines[2:]

boards = [
    [list(map(int, filter(None, row.split()))) for row in lines[i:i + 5]]
    for i in range(0, len(lines), 6)
]


found = False
for i in range(1, len(nums)):
    n = nums[:i]
    for board in boards.copy():
        for b in (board, zip(*board)):
            b = [[x for x in row if x not in n] for row in b]
            if not all(b):  # if empty row found
                # part 1
                if not found:
                    print(sum(map(sum, b)) * nums[i - 1])
                    found = True
                # part 2
                if len(boards) == 1:
                    print(sum(map(sum, b)) * nums[i - 1])
                boards.remove(board)
                break
# if empty row found # part 1 # part 2