2020 day 20-22
This commit is contained in:
50
2020/d21.py
Normal file
50
2020/d21.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from collections import defaultdict
|
||||
from lib import get_data
|
||||
|
||||
data = get_data(__file__)
|
||||
|
||||
algs_to_ings = defaultdict(set)
|
||||
ings_counts = defaultdict(int)
|
||||
|
||||
for line in data.splitlines():
|
||||
a, b = line.split(" (contains ")
|
||||
ings = a.split(" ")
|
||||
algs = b[:-1].split(", ")
|
||||
for ing in ings:
|
||||
ings_counts[ing] += 1
|
||||
|
||||
for a in algs:
|
||||
if a not in algs_to_ings:
|
||||
algs_to_ings[a] = set(ings)
|
||||
else:
|
||||
algs_to_ings[a] &= set(ings)
|
||||
|
||||
|
||||
for ings in algs_to_ings.values():
|
||||
for ing in ings:
|
||||
if ing in ings_counts:
|
||||
del ings_counts[ing]
|
||||
t = sum(ings_counts.values())
|
||||
print(t)
|
||||
|
||||
|
||||
algs_to_ings = {a: list(ing) for a, ing in algs_to_ings.items()}
|
||||
|
||||
handled = set()
|
||||
while True:
|
||||
to_remove = None
|
||||
for alg, ings in algs_to_ings.items():
|
||||
if len(ings) == 1 and ings[0] not in handled:
|
||||
to_remove = ings[0]
|
||||
break
|
||||
else:
|
||||
break
|
||||
assert to_remove is not None
|
||||
handled.add(to_remove)
|
||||
for ings in algs_to_ings.values():
|
||||
if len(ings) > 1 and to_remove in ings:
|
||||
ings.remove(to_remove)
|
||||
|
||||
xs = sorted([(a, i[0]) for a, i in algs_to_ings.items()])
|
||||
xs = ",".join([x[1] for x in xs])
|
||||
print(xs)
|
||||
Reference in New Issue
Block a user