day 4 part 2
parent
33171e5512
commit
1ee568b507
16
day04/sol.py
16
day04/sol.py
|
@ -1,4 +1,5 @@
|
||||||
import sys
|
import sys
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
def matches2score(n):
|
def matches2score(n):
|
||||||
if n <= 0:
|
if n <= 0:
|
||||||
|
@ -7,15 +8,30 @@ def matches2score(n):
|
||||||
|
|
||||||
input = sys.stdin
|
input = sys.stdin
|
||||||
part1 = 0
|
part1 = 0
|
||||||
|
card = 0
|
||||||
|
copies = defaultdict(int)
|
||||||
for line in input:
|
for line in input:
|
||||||
|
card += 1
|
||||||
_, _, data = line.partition(": ")
|
_, _, data = line.partition(": ")
|
||||||
winners, numbers = data.split("|")
|
winners, numbers = data.split("|")
|
||||||
winners = map(int, winners.split())
|
winners = map(int, winners.split())
|
||||||
numbers = map(int, numbers.split())
|
numbers = map(int, numbers.split())
|
||||||
|
|
||||||
matches = len(set(winners) & set(numbers))
|
matches = len(set(winners) & set(numbers))
|
||||||
|
|
||||||
s = matches2score(matches)
|
s = matches2score(matches)
|
||||||
print(s)
|
print(s)
|
||||||
part1 += s
|
part1 += s
|
||||||
|
|
||||||
|
copies[card] += 1 # original copy
|
||||||
|
for i in range(matches):
|
||||||
|
copies[card+i+1] += copies[card]
|
||||||
|
|
||||||
|
print("-")
|
||||||
|
|
||||||
print(part1)
|
print(part1)
|
||||||
|
|
||||||
|
assert not [i for i,c in copies.items() if c > 0 and i > card]
|
||||||
|
|
||||||
|
part2 = sum(copies.values())
|
||||||
|
print(part2)
|
||||||
|
|
Loading…
Reference in New Issue