aoc/2023/04/04.py

29 lines
710 B
Python
Raw Permalink Normal View History

2024-03-05 09:32:18 +00:00
with open("input.txt") as f:
lines = f.readlines()
part1 = 0
for line in lines:
_, card = line.split(": ")
winning, yours = card.split(" | ")
winning = {int(n) for n in winning.split()}
yours = {int(n) for n in yours.split()}
how_many_wins = len(winning & yours)
worth = 2 ** (how_many_wins - 1) if how_many_wins >= 1 else 0
part1 += worth
print(part1)
cards = [1]*len(lines)
for i, line in enumerate(lines):
_, card = line.split(": ")
winning, yours = card.split(" | ")
winning = {int(n) for n in winning.split()}
yours = {int(n) for n in yours.split()}
how_many_wins = len(winning & yours)
for n in range(i+1, i+1+how_many_wins):
cards[n] += 1 * cards[i]
part2 = sum(cards)
print(part2)