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.

Update 2024/05

+7 -9
+7 -9
2024/day05.py
··· 16 16 return lst[(len(lst) - 1) // 2] 17 17 18 18 19 + # Parse problem input. 19 20 on_updates = False 20 21 graph = defaultdict(set) 21 22 updates = [] ··· 36 37 if is_unordered(graph, update) == -1: 37 38 part_1 += mid(update) 38 39 else: 39 - while True: 40 - swap = False 41 - for i, n in enumerate(update): 42 - afters = update[i+1:] 43 - if any(n in graph[a] for a in afters): 44 - update[i], update[i + 1] = update[i + 1], update[i] 45 - swap = True 46 - if not swap: 47 - break 40 + # Sort based on how many pages in `updates` should come after a given page. 41 + afters = {} 42 + for i, n in enumerate(update): 43 + afters[n] = len(graph[n] & (set(update) - set([n]))) 44 + 45 + update.sort(key=afters.get) 48 46 49 47 part_2 += mid(update) 50 48