Something went wrong. Try again.
My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code
advent-of-code python
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import fileinputfrom collections import OrderedDict
# Read inputdata = ''.join([line for line in fileinput.input()])groups = [g.split('\n') for g in data.split('\n\n')]
order = groups[0]order = [int(x) for x in order[0].split(',')]
boards = groups[1:]
BOARDS = []for b in boards: board = [[int(x) for x in line.split()] for line in b] BOARDS.append(board)
MARKS = [[[False for _ in range(5)] for _ in range(5)] for _ in range(len(BOARDS))]
def is_win(marks): for line in marks: if all(line): return True
for line in zip(*marks): if all(line): return True
return False
scores = OrderedDict()
for n in order: for board, marks in zip(BOARDS, MARKS): for y, line in enumerate(board): for x, val in enumerate(line): if n == val: marks[y][x] = True
# Check for winners for i, b in enumerate(MARKS): if is_win(b) and i not in scores: unmarked = 0 for y, line in enumerate(MARKS[i]): for x, val in enumerate(line): if not val: unmarked += BOARDS[i][y][x]
scores[i] = unmarked * n
print "Part 1:", scores.values()[0]print "Part 2:", scores.values()[-1]