2023 #1
1 changed files with 50 additions and 0 deletions
50
2023-python/output/day_04.py
Normal file
50
2023-python/output/day_04.py
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
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
|
||||||
Loading…
Add table
Reference in a new issue