day 4 part 2 (in Python)
This commit is contained in:
parent
5aa6f8235d
commit
8ff5e7f9c9
1 changed files with 17 additions and 8 deletions
25
solve.py
25
solve.py
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue