On a train that according to swedish tradition was late. Not a good environment to focus. Got stuck 2 times: - Initial code asumed the | was always after the 5th number, because of the example. Puzzle input had it at pos 10. Classic AoC mistake. - I had a hard time trying to understand the score count, I insisted there was meant to be a +1 at some point. > That means card 1 is worth 8 points (1 for > the first match, then doubled three times for > each of the three matches after the first) I should instead have just looked at the numbers.
50 lines
1 KiB
Python
50 lines
1 KiB
Python
import re
|
|
from collections import defaultdict
|
|
from output import answer, puzzleinput
|
|
|
|
n = 4
|
|
title = "Scratchcards"
|
|
|
|
|
|
@answer(1, "Sum of all scratchcard points are {}")
|
|
def part_1(presolved):
|
|
scores, _ = presolved
|
|
return scores
|
|
|
|
|
|
@answer(2, "Ends up wih a total of {} scratchcards")
|
|
def part_2(presolved):
|
|
_, count = presolved
|
|
return count
|
|
|
|
|
|
@puzzleinput(n)
|
|
def parse_input(data):
|
|
return data
|
|
|
|
|
|
def presolve(data):
|
|
scores = []
|
|
count = defaultdict(int)
|
|
for cid, line in enumerate(data.splitlines()):
|
|
a, b = line.split("|")
|
|
a = set(re.findall(r"\d+", a)[1:])
|
|
b = set(re.findall(r"\d+", b))
|
|
ab = len(a & b)
|
|
if ab > 0:
|
|
scores.append(2**(ab - 1))
|
|
count[cid] += 1
|
|
for i in range(cid + 1, cid + ab + 1):
|
|
count[i] += count[cid]
|
|
return sum(scores), sum(count.values())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
inp = parse_input()
|
|
inp = presolve(inp)
|
|
|
|
a = part_1(inp)
|
|
b = part_2(inp)
|
|
|
|
assert a == 21919
|
|
assert b == 9881048
|