[Python/2017] Move solutions into .py files
This commit is contained in:
parent
fbc5fda60f
commit
7b1efc0d9c
51 changed files with 1100 additions and 4546 deletions
52
Python/2017/07.py
Normal file
52
Python/2017/07.py
Normal file
|
@ -0,0 +1,52 @@
|
|||
from lib import *
|
||||
|
||||
input = read_input(2017, 7)
|
||||
|
||||
lines = input.splitlines()
|
||||
|
||||
|
||||
names = set()
|
||||
subs = set()
|
||||
for line in lines:
|
||||
name = line.split()[0]
|
||||
sub = [] if "->" not in line else line.split("-> ")[1].split(", ")
|
||||
names.add(name)
|
||||
subs.update(sub)
|
||||
|
||||
print(next(iter(names - subs)))
|
||||
|
||||
|
||||
progs = {}
|
||||
names = set()
|
||||
subs = set()
|
||||
for line in lines:
|
||||
name = line.split()[0]
|
||||
weight = int(line.split("(")[1].split(")")[0])
|
||||
sub = [] if "->" not in line else line.split("-> ")[1].split(", ")
|
||||
progs[name] = weight, sub
|
||||
names.add(name)
|
||||
subs.update(sub)
|
||||
|
||||
root = next(iter(names - subs))
|
||||
|
||||
|
||||
def check_sum(p):
|
||||
s = progs[p][0]
|
||||
w = {}
|
||||
for q in progs[p][1]:
|
||||
a, b = check_sum(q)
|
||||
if a is not None:
|
||||
return a, None
|
||||
s += b
|
||||
w[q] = b
|
||||
|
||||
x = max(w.values(), key=list(w.values()).count, default=None)
|
||||
|
||||
for k, v in w.items():
|
||||
if v != x:
|
||||
return x - (v - progs[k][0]), None
|
||||
|
||||
return None, s
|
||||
|
||||
|
||||
print(check_sum(root)[0])
|
Loading…
Add table
Add a link
Reference in a new issue