def solve79(): f = open("p079_keylog.txt") subs = [line.rstrip() for line in f] dependencies = [] for sub in subs: dependencies += build_dependencies(sub) dependencies = set(dependencies) keywords = [subs[0]] for d in dependencies: keywords = list(set(gen_keywords(d, keywords))) return shortest(keywords)
def gen_keywords(dep, words): keywords = [] for word in words: if satisfies_dependency(word, dep): keywords.append(word) else: if dep[0] in word: for x in range(word.index(dep[0]), len(word)): keywords.append(word[:x+1]+dep[1]+word[x+1:]) elif dep[1] in word: for x in range(word.index(dep[1])+1): keywords.append(word[:x]+dep[0]+word[x:]) else: for x in range(len(word)): keywords.append(word[:x]+dep[0]+word[x:]) keywords = gen_keywords(dep, keywords) s = len(shortest(keywords)) return [k for k in keywords if len(k)==s]