aoc

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

common.py (847B) [raw]


      1 import sys
      2 
      3 def parse():
      4     first = True
      5     N = None
      6     boards = []
      7     board = []
      8     for l in sys.stdin:
      9         if first:
     10             N = [int(x) for x in l.strip().split(',')]
     11             first = False
     12         else:
     13             l = l.strip()
     14             if l:
     15                 row = [int(x) for x in l.split()]
     16                 board.append(row)
     17             elif board:
     18                 boards.append(board)
     19                 board = []
     20 
     21     return N, boards
     22 
     23 def index(boards):
     24     # Generates a lookup table of:
     25     #   num_called => (board, row, column)
     26     index = {}
     27     i = 0
     28     for b in boards:
     29         for r in range(len(b)):
     30             for c in range(len(b[r])):
     31                 x = b[r][c]
     32                 if x not in index:
     33                     index[x] = []
     34                 index[x].append((i, r, c))
     35         i+=1
     36 
     37     return index