day 4 part 2 (in Python)

This commit is contained in:
Andy Teijelo 2023-12-04 13:05:07 -05:00
parent 5aa6f8235d
commit 8ff5e7f9c9

View file

@ -1,22 +1,31 @@
#!/usr/bin/env python3
import sys
from dataclasses import dataclass
@dataclass
class Row:
cards: int
matches: int
sum = 0
rows = []
for line in sys.stdin:
_, nums = line.split(":")
winning, mine = nums.split("|")
winning = set(winning.split())
if len(winning) != len(set(winning)):
raise ValueError("Shenannigans!")
mine = set(mine.split())
if len(mine) != len(set(mine)):
raise ValueError("Shenannigans! {}".format(mine))
both = winning.intersection(mine)
c = len(both)
if c > 0:
sum += 2 ** (c - 1)
rows.append(Row(matches=len(both), cards=1))
for i in range(len(rows)):
m = rows[i]
for j in range(m.matches):
rows[i + 1 + j].cards += m.cards
for row in rows:
sum += row.cards
print(row)
print(sum)