#!/usr/bin/env python3 import sys import common from collections import defaultdict def main(boards, N): # we need an efficient way to find boards with numbers # get a mapping of num_called -> (board_idx, row, col) index = common.index(boards) # now go through all numbers and keep track of if r/c counts # by board, if one is 5... seen = {} counts = {} for n in N: seen[n] = 1 if n not in index: continue for b, r, c in index[n]: if b not in counts: counts[b] = defaultdict(int) counts[b][f"r{r}"] += 1 counts[b][f"c{c}"] += 1 # check win condition for v in counts[b].values(): if v == 5: # find all unmarked in board... s = 0 for row in boards[b]: for x in row: if x not in seen: s += x print(n * s) sys.exit(0) if __name__ == '__main__': N, boards = common.parse() main(boards, N)