diff --git a/solve.py b/solve.py index b3dc3ca..6ab0db5 100755 --- a/solve.py +++ b/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)