My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add 2022/06

+49
+17
2022/day06.py
··· 1 + import fileinput 2 + 3 + stream = fileinput.input()[0].strip() 4 + 5 + part_1 = None 6 + part_2 = None 7 + for i in range(len(stream)): 8 + window_1 = stream[i:i+4] 9 + window_2 = stream[i:i+14] 10 + if len(window_1) == len(set(window_1)) and not part_1: 11 + part_1 = i + 4 12 + if len(window_2) == len(set(window_2)) and not part_2: 13 + part_2 = i + 14 14 + break 15 + 16 + print("Part 1:", part_1) 17 + print("Part 2:", part_2)
+32
2022/utils.py
··· 82 82 return len(lst) == len(set(lst)) 83 83 84 84 85 + def topsort(graph, tiebreak=None): 86 + """ 87 + Given a graph where graph[x] is an iterable of edges of directed 88 + edges originating from x, returns a topologically sorted list of 89 + nodes in the graph. 90 + 91 + If `tiebreak` is given, this lambda is passed to sorted() when 92 + choosing what node to visit next. 93 + """ 94 + if tiebreak is None: 95 + tiebreak = lambda x: x 96 + 97 + visited = set() 98 + stack = [] 99 + 100 + def _topsort(node): 101 + visited.add(node) 102 + 103 + # Reversed because the DFS causes equal level nodes to be popped backwards. 104 + for n in sorted(graph[node], key=tiebreak, reverse=True): 105 + if n not in visited: 106 + _topsort(n) 107 + 108 + stack.append(node) 109 + 110 + for n in sorted(graph, key=tiebreak, reverse=True): 111 + if not n in visited: 112 + _topsort(n) 113 + 114 + return stack[::-1] 115 + 116 + 85 117 def gcd(a,b): 86 118 """Compute the greatest common divisor of a and b""" 87 119 while b > 0: