input = open("Crifouinette.txt").read() # input = """1: 2, 4, 6 # 2: 1, 3 # 3: 2, 4, 5 # 4: 1, 3, 5 # 5: 3, 4 # 6: 1, 7, 8 # 7: 6 # 8: 6""" fouinettes = dict() for i in input.splitlines(): f, f_contact = i.replace(" ", "").split(":") fouinettes[int(f)] = [int(v) for v in list(f_contact.split(","))] solution = dict() for start in fouinettes: print(start) max_step = 0 f_alertes = [start, ] #La liste des fouinettes déjà alertés new_f_alertes = [start, ] #La liste des fouinette nouvellement alertés while f_alertes != list(fouinettes.keys()): max_step += 1 new_f_alertes_tmp = list() # print("previous:", new_f_alertes) #On cherche les nouveaux fouinettes alertés à l'étape suivante for f in new_f_alertes: new_f_alertes_tmp = new_f_alertes_tmp + fouinettes[f] #On supprive ceux déjà altertés auparavant et les doublons new_f_alertes_tmp = sorted(list(set([item for item in new_f_alertes_tmp if item not in f_alertes]))) if len(new_f_alertes_tmp) == 0: raise ValueError("new_f_alertes_tmp empty") # print("new:", new_f_alertes_tmp) f_alertes = sorted(f_alertes + new_f_alertes_tmp) new_f_alertes = new_f_alertes_tmp # print("all alerted:", f_alertes) # print() if max_step not in solution: solution[max_step] = sorted(new_f_alertes + [start, ]) else: solution[max_step] = sorted(list(set((solution[max_step] + new_f_alertes + [start, ])))) print(max(solution.keys()), solution[max(solution.keys())])