2023/day04.py

28 lines
562 B
Python
Raw Normal View History

2023-12-05 13:01:55 +00:00
import fileinput
2023-12-05 13:19:16 +00:00
def main():
#
# Part 1
#
cards = []
part1 = 0
for line in fileinput.input():
card = line.split(": ", 1)[1]
(winning, owned) = ({int(i) for i in h.split()} for h in card.split(" | ", 1))
count = len(winning & owned)
cards.append(count)
part1 += 1 << count - 1 if count else 0
2023-12-05 13:01:55 +00:00
2023-12-05 13:19:16 +00:00
#
# 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]
2023-12-05 13:01:55 +00:00
2023-12-05 13:19:16 +00:00
print(part1, part2)
main()