Part 2
This commit is contained in:
parent
bf9a54c823
commit
34525a73b8
2 changed files with 68 additions and 0 deletions
1
day_4/answer2
Normal file
1
day_4/answer2
Normal file
|
@ -0,0 +1 @@
|
||||||
|
5667240
|
67
day_4/part2.py
Normal file
67
day_4/part2.py
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
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()))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue