Skip to content

Instantly share code, notes, and snippets.

@spielkind
Created December 5, 2021 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spielkind/4b4df7ae18f9ced80ea2b4d593635b3d to your computer and use it in GitHub Desktop.
Save spielkind/4b4df7ae18f9ced80ea2b4d593635b3d to your computer and use it in GitHub Desktop.
#!/bin/python
with open('day4.txt') as f:
numbers = list(map(int, f.readline().strip().split(',')))
player = -1
bingocards = []
matches = []
for line in f:
if line.strip() == '':
player += 1
bingocards.append([])
matches.append([])
else:
bingocards[player].append(list(map(int, line.strip().split())))
matches[player].append([0 for i in range(len(bingocards[player][-1]))])
def check_bingo(bingocard):
for x in range(len(bingocard)):
if sum([bingocard[y][x] for y in range(len(bingocard))]) == len(bingocard):
return True
if sum([bingocard[x][y] for y in range(len(bingocard))]) == len(bingocard):
return True
return False
def play_bingo():
winners = []
for n in numbers:
for pi, p in enumerate(bingocards):
if pi not in [w[0] for w in winners]:
for ri, r in enumerate(p):
for vi, v in enumerate(r):
if n == bingocards[pi][ri][vi]:
matches[pi][ri][vi] = 1
if check_bingo(matches[pi]):
winners.append([pi, n])
return winners
def calc_win(result):
unmatched = []
for ri, r in enumerate(bingocards[result[0]]):
for vi, v in enumerate(r):
if matches[result[0]][ri][vi] == 0:
unmatched.append(v)
return sum(unmatched*result[1])
winners = play_bingo()
print('Part One:', calc_win(winners[0]))
print('Part Two:', calc_win(winners[-1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment