from itertools import product input = open("AmisEwoks.txt").read().splitlines() #list of ewoks table = dict() #list of direct links couples = list() print("d =", 0) for i in input: a,b = i.split(",") a = a.strip() b = b.strip() if a not in table: table[a] = dict() table[a][a] = 0 if b not in table: table[b] = dict() table[b][b] = 0 couples.append((a,b)) print("\t", "new links:", len(table)) print("d =", 1) for ewok in table: for couple in couples: if couple[0] == ewok: table[ewok][couple[1]] = 1 table[couple[1]][ewok] = 1 if couple[0] == ewok: table[ewok][couple[0]] = 1 table[couple[0]][ewok] = 1 print("\t", "new links:", len(couples)) #find new links, 8 times as we know that the farest eworks are d = 8 max_d = 0 for i in range(3): print("d =", i+2) new_links = dict() for ewok1 in table: couples_knowing_ewok1 = list(product(table[ewok1].keys(), repeat=2)) for couple in couples_knowing_ewok1: a = couple[0] b = couple[1] da = table[ewok1][a] db = table[ewok1][a] if (a,b) not in new_links and (b,a) not in new_links and a not in table[b]: new_links[(a,b)]=da+db max_d = max(max_d, da+db) elif (a,b) in new_links: new_links[(a,b)]=min(da+db, new_links[(a,b)]) elif (b,a) in new_links: new_links[(b,a)]=min(da+db, new_links[(b,a)]) #Add new lins to the table print("\t", "new links:", len(new_links)) for c1, c2 in new_links: table[c1][c2] = new_links[(c1,c2)] table[c2][c1] = new_links[(c1,c2)] nb_links = 0 for ewok1 in table: for ewok2 in table[ewok1]: nb_links += 1 print("nb_links", nb_links) print("max_d", max_d) solution = dict() for ewok1 in table: for ewok2 in table[ewok1]: if table[ewok1][ewok2] == max_d: solution[ewok1] = None solution[ewok2] = None print('"' + '", "'.join(solution.keys())+ '"') """ input = sorted(input) solutions = dict() ewoks = list() couples = list() for i in input: a,b = i.split(",") couples.append((a.strip(), b.strip())) solutions[(a.strip(), b.strip())] = 1 if a not in ewoks: ewoks.append(a) if b not in ewoks: ewoks.append(b) # for i in range(len(ewoks)): for i in range(len(ewoks)): for j in range(i+1, len(ewoks)): start = ewoks[i] stop = ewoks[j] if (start, stop) in solutions or (stop, start) in solutions: print("X", end='') continue 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 found = True #On popule la liste de résultats avec tous les éléments de la chaine for a in range(len(chain)): for b in range(len(chain)): coup = (chain[a],chain[b]) dist = abs(b-a) solutions[coup] = dist print(len(solutions), coup, ":", dist) # print() # for k in solutions: # print(k, solutions[k]) for res in solutions: if solutions[res]>=8: print(res) """