aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

b.py (1349B) [raw]


      1 #!/usr/bin/env python3
      2 import sys
      3 import common
      4 from collections import defaultdict
      5 
      6 def main(boards, N):
      7     # we need an efficient way to find boards with numbers
      8     # get a mapping of num_called -> (board_idx, row, col)
      9     index = common.index(boards)
     10 
     11     # now go through all numbers and keep track of if r/c counts
     12     # by board, if one is 5, we have a winner!
     13     winners = {}
     14     seen = {}
     15     counts = {}
     16     for n in N:
     17         seen[n] = 1
     18         if n not in index:
     19             continue
     20         for b, r, c in index[n]:
     21             if b in winners:
     22                 continue
     23 
     24             if b not in counts:
     25                 counts[b] = defaultdict(int)
     26             counts[b][f"r{r}"] += 1
     27             counts[b][f"c{c}"] += 1
     28 
     29             # check win condition
     30             for v in counts[b].values():
     31                 if v == 5:
     32                     winners[b] = 1
     33                     if len(winners) == len(boards):
     34                         # find all unmarked in board...
     35                         s = 0
     36                         for row in boards[b]:
     37                             for x in row:
     38                                 if x not in seen:
     39                                     s += x
     40 
     41                         print(n * s)
     42                         sys.exit(0)
     43 
     44 
     45 if __name__ == '__main__':
     46     N, boards = common.parse()
     47     main(boards, N)