Skip to content

Instantly share code, notes, and snippets.

@pawlos
Last active December 24, 2017 18:49
Show Gist options
  • Save pawlos/014bb51611a7b62fe354f1e7c53a6e08 to your computer and use it in GitHub Desktop.
Save pawlos/014bb51611a7b62fe354f1e7c53a6e08 to your computer and use it in GitHub Desktop.
Solution to Day 7: Recursive Circus - part 2
#aoc_d7.py
inp = open('input_d7.txt','r').readlines()
'''inp = ["pbga (66)",
"xhth (57)",
"ebii (61)",
"havc (66)",
"ktlj (57)",
"fwft (72) -> ktlj, cntj, xhth",
"qoyq (66)",
"padx (45) -> pbga, havc, qoyq",
"tknk (41) -> ugml, padx, fwft",
"jptl (61)",
"ugml (68) -> gyxo, ebii, jptl",
"gyxo (61)",
"cntj (57)"]
'''
old = inp
dic = dict()
new_l = []
for item in inp:
if "->" not in item:
idx = item.index('(')
name = item[0:idx].strip()
weight = item[idx+1:item.index(')')]
dic[name] = weight
else:
new_l.append(item)
#print 'Inp:', new_l
cont = True
while cont:
cont = False
#print 'F'
for item in new_l:
if "->" in item:
items = item.split("->")
idx = items[0].index('(')
name = items[0][0:idx].strip()
weight = item[idx+1:items[0].index(')')]
children = [i.strip() for i in items[1].split(',')]
dic[name] = int(weight)
#print children, dic
if any([w not in dic for w in children]):
#print 'c'
cont = True
continue
s = sum([int(dic[w]) for w in children])
dic[name] += s
#print s
#print dic
#print 'old',old
for it in old:
#print "Check", it
if "->" in it:
items = it.split("->")
idx = items[0].index('(')
name = items[0][0:idx].strip()
weight = dic[name]
children = [i.strip() for i in items[1].split(',')]
pattern = int(dic[children[0]])
m = all([int(dic[w])==pattern for w in children])
if not m:
print name, weight, children, [int(dic[w]) for w in children]
print "found?", pattern
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment