advent-of-code/2023/python/day04.py

60 lines
1.5 KiB
Python
Raw Normal View History

2023-12-04 15:30:55 +00:00
import shared
import re
from pprint import pprint
pattern = r"Card\s+(\d+): ([\d\s]+) \| ([\d\s]+)"
def load_cards(rows):
cards = {}
for row in rows:
match = re.match(pattern, row)
card_number = int(match.group(1))
winning_numbers = set(list(map(int, match.group(2).split())))
have_numbers = set(list(map(int, match.group(3).split())))
have = winning_numbers.intersection(have_numbers)
cards[card_number] = dict(
winning_numbers=winning_numbers,
have_numbers=have_numbers,
have=have,
have_count=len(have),
points=0
)
if have:
points = 1
for _ in range(len(have)-1):
points *= 2
cards[card_number]['points'] = points
return cards
# @shared.profile
def part1(cards):
print(sum(card['points'] for _, card in cards.items()))
print(cards[1].keys())
# @shared.profile
def part2(cards):
card_ids = list(range(len(cards)+1))
# 'winning_numbers', 'have_numbers', 'have', 'have_count', 'points'
for card_id, card in cards.items():
nn = [x+card_id for x in range(card['have_count'])]
breakpoint()
def main():
rows = [row for row in shared.load_rows(4)]
cards = load_cards(rows)
with shared.elapsed_timer() as elapsed:
part1(cards)
print("🕒", elapsed())
with shared.elapsed_timer() as elapsed:
part2(cards)
print("🕒", elapsed())
if __name__ == "__main__":
main()