input = open("ciceron.txt").read().splitlines() start = "Elyesa Bazna" stop = "Ludwig Carl Moyzisch" couples = list() for i in input: a,b = i.split("-") couples.append((a.strip(), b.strip())) chains = [[start,]] found = False while not found: print(".", end='') for chain in chains: latest = chain[-1] nexts = list() #cherche les nouveaux contacts for c in couples: if latest==c[0] and c[1] not in nexts: nexts.append(c[1]) elif latest==c[1] and c[0] not in nexts: nexts.append(c[0]) #Supprime les nouveaux contacts déjà présents dans la chaine for maillon in chain: if maillon in nexts: nexts.remove(maillon) #Ajoute la liste de nouveaux contacts à la chaine chain.append(nexts) #Creation d'une nouvelle chaine pour optimiser la suite en évitant des boucles new_chains = list() #Pour chaque chaine for chain in chains: #Si le chaine se termine par une liste de contact if isinstance(chain[-1], list): #Crée une liste de contacts à garder children = list() #Pour chaque nouveau contact for child in chain[-1]: #On vérifie qu'ion n'est pas présent dans une autre chaine for other_chain in chains: #Si pas déjà présent, on le garde if child not in other_chain and child not in children: children.append(child) #Pour chaque contact à garder, on crée une nouvelle chaine égale à l'ancienne + le nouveau contact for child in children: new_chains.append(chain[:-1]+[child,]) #La chaine ne se termine pas par une liste, c'est un cul de sac else: # new_chains.append(chain) pass #chains est maintenant égal à la nouvelle chain chains = new_chains #Si la cible est dans une chaine, c'est gagné! for chain in chains: if stop in chain: solution = chain[1:-1] found = True print() print('"' + '", "'.join(solution) + '"')