41 lines
757 B
Python
41 lines
757 B
Python
""" day 5 part 1 """
|
|
|
|
import fileinput
|
|
import collections
|
|
|
|
deps = collections.defaultdict(set) # map post -> [pre]
|
|
|
|
reader = fileinput.input()
|
|
|
|
for line in reader:
|
|
line = line.strip()
|
|
if not line:
|
|
break
|
|
|
|
pre,post = line.split('|')
|
|
deps[post].add(pre)
|
|
|
|
total = 0
|
|
|
|
for line in reader:
|
|
seq = line.strip().split(',')
|
|
wanted = set(seq)
|
|
|
|
seen = set()
|
|
safe = True
|
|
for page in seq:
|
|
# print(f'{seen} -> {page}')
|
|
for dep in deps[page]:
|
|
if dep in wanted and dep not in seen:
|
|
# print(f' {page} missing {dep}')
|
|
safe = False
|
|
seen.add(page)
|
|
|
|
val = seq[len(seq)//2]
|
|
print(f'{safe}: {seq} ({val})')
|
|
if safe:
|
|
total += int(val)
|
|
|
|
print(total)
|
|
|