My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add solution for 2023/04

+32
+32
2023/day04.py
··· 1 + import fileinput 2 + from collections import Counter 3 + 4 + 5 + part_1 = 0 6 + part_2_cards = Counter() 7 + 8 + for line in fileinput.input(): 9 + card, rest = line.split(': ') 10 + card_id = int(card[5:]) 11 + 12 + winning, your = rest.split(' | ') 13 + winning = set(int(n) for n in winning.split()) 14 + your = [int(n) for n in your.split()] 15 + 16 + # Add the "original" scratchcard as one of the copies. 17 + part_2_cards[card_id] += 1 18 + 19 + matches = 0 20 + for n in your: 21 + if n in winning: 22 + matches += 1 23 + 24 + if matches > 0: 25 + part_1 += 2 ** (matches - 1) 26 + 27 + for copy_id in range(card_id + 1, card_id + matches + 1): 28 + part_2_cards[copy_id] += part_2_cards[card_id] 29 + 30 + print("Part 1:", part_1) 31 + print("Part 2:", sum(part_2_cards.values())) 32 +