aoc

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

a.py (1143B) [raw]


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