Add day04.py
This commit is contained in:
parent
9c48228a3f
commit
85c54edbe1
1 changed files with 24 additions and 0 deletions
24
day04.py
Normal file
24
day04.py
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
import fileinput
|
||||||
|
|
||||||
|
#
|
||||||
|
# Part 1
|
||||||
|
#
|
||||||
|
cards = []
|
||||||
|
part1 = 0
|
||||||
|
for i, line in enumerate(fileinput.input()):
|
||||||
|
card = line.rstrip().split(": ")[1]
|
||||||
|
(winning, owned) = ({int(i) for i in h.split()} for h in card.split(" | "))
|
||||||
|
count = len(winning & owned)
|
||||||
|
cards.append(count)
|
||||||
|
part1 += 1 << count - 1 if count else 0
|
||||||
|
|
||||||
|
#
|
||||||
|
# Part 2
|
||||||
|
#
|
||||||
|
dp = [0] * len(cards)
|
||||||
|
part2 = 0
|
||||||
|
for i in range(len(cards)):
|
||||||
|
dp[i] = 1 + sum(dp[i - cards[-i - 1] : i])
|
||||||
|
part2 += dp[i]
|
||||||
|
|
||||||
|
print(part1, part2)
|
Loading…
Reference in a new issue