···11+import fileinput
22+33+stream = fileinput.input()[0].strip()
44+55+part_1 = None
66+part_2 = None
77+for i in range(len(stream)):
88+ window_1 = stream[i:i+4]
99+ window_2 = stream[i:i+14]
1010+ if len(window_1) == len(set(window_1)) and not part_1:
1111+ part_1 = i + 4
1212+ if len(window_2) == len(set(window_2)) and not part_2:
1313+ part_2 = i + 14
1414+ break
1515+1616+print("Part 1:", part_1)
1717+print("Part 2:", part_2)
+32
2022/utils.py
···8282 return len(lst) == len(set(lst))
838384848585+def topsort(graph, tiebreak=None):
8686+ """
8787+ Given a graph where graph[x] is an iterable of edges of directed
8888+ edges originating from x, returns a topologically sorted list of
8989+ nodes in the graph.
9090+9191+ If `tiebreak` is given, this lambda is passed to sorted() when
9292+ choosing what node to visit next.
9393+ """
9494+ if tiebreak is None:
9595+ tiebreak = lambda x: x
9696+9797+ visited = set()
9898+ stack = []
9999+100100+ def _topsort(node):
101101+ visited.add(node)
102102+103103+ # Reversed because the DFS causes equal level nodes to be popped backwards.
104104+ for n in sorted(graph[node], key=tiebreak, reverse=True):
105105+ if n not in visited:
106106+ _topsort(n)
107107+108108+ stack.append(node)
109109+110110+ for n in sorted(graph, key=tiebreak, reverse=True):
111111+ if not n in visited:
112112+ _topsort(n)
113113+114114+ return stack[::-1]
115115+116116+85117def gcd(a,b):
86118 """Compute the greatest common divisor of a and b"""
87119 while b > 0: