1
0
Fork 0

Puzzle du 7 décembre 2017

This commit is contained in:
Lucidiot 2017-12-07 19:07:05 +01:00
parent 81b461493a
commit d1015659f9
No known key found for this signature in database
GPG Key ID: 63BD9482C29D0F64
1 changed files with 33 additions and 0 deletions

33
2017/7/tower.py Normal file
View File

@ -0,0 +1,33 @@
#!/usr/bin/env python3
import sys, re
from collections import Counter
pattern = re.compile(r"([a-z]+) \(([0-9]+)\)( -> (.+))?")
data = [pattern.findall(line.strip())[0] for line in sys.stdin.readlines()]
programs = {}
for line in data:
try:
programs[line[0]] = re.split(r', ', line[2][4:])
if programs[line[0]] == ['']:
programs[line[0]] == []
except IndexError:
programs[line[0]] = []
for prog in programs:
if any(prog in programs[p] for p in programs):
continue
root = prog
def weight(program):
thisweight = [int(line[1]) for line in data if line[0] == program][0]
if len(programs[program]) > 0 and '' not in programs[program]:
return thisweight + sum(weight(sub) for sub in programs[program])
return thisweight
def find_wrong_weight(program):
subs = {k: v for (k, v) in weights if k in programs[program]}
if len(set(subs.values())) > 1:
return find_wrong_weight(list(subs.keys())[list(subs.values()).index(Counter(subs.values()).most_common(2)[1][0])])
return (program, weight(program))
weights = {(p, weight(p)) for p in programs}
wrong = find_wrong_weight(root)
print(next({k: v for (k, v) in weights if k in programs[p]} for p in programs if wrong[0] in programs[p]))