Advertisement
hhoppe

Advent of code 2023 day 4

Dec 3rd, 2023
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.46 KB | None | 0 0
  1. def day4(s, *, part2=False):
  2.   copies = collections.Counter()
  3.   total = 0
  4.  
  5.   for card_id, line in enumerate(s.splitlines()):
  6.     winning, have = map(str.split, line.split(':')[1].split(' | '))
  7.     num_matches = len(set(winning) & set(have))
  8.     if part2:
  9.       value = 1 + copies[card_id]
  10.       for i in range(num_matches):
  11.         copies[card_id + 1 + i] += value
  12.       total += value
  13.     elif num_matches:
  14.       total += 2 ** (num_matches - 1)
  15.  
  16.   return total
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement