day 04
scratchcards
approach
each card is parsed into two sets of numbers, and the matches are their intersection.
part 1 scores a card as 1 << (matches - 1) when it has any matches. part 2 keeps a list with the number of copies of every card, starting at one each. walking the cards in order, a card with k matches adds its current copy count to each of the next k cards, and the answer is the sum of the list.
the trick
process cards in order and push each card's copy count forward instead of simulating every copy.
python
def part2(games):
res = 0
copies = [1 for _ in range(len(games))]
for i, (winning, have) in enumerate(games):
inter = have & winning
for j in range(1, len(inter) + 1):
copies[i + j] += copies[i]
return sum(copies)