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.

Optimize 2017/24 solution

Remove components of the form `X/X` from the graph, which reduces
runtime from ~10s to ~500ms.

+13 -5
+13 -5
2017/day24.py
··· 25 25 next_ports = ports + [port] 26 26 strength = bridge_strength(next_ports) 27 27 28 + # See if we can incorporate any `X/X` components 29 + strength += sum(2 * p for p in BRIDGE_EXTRAS if p in next_ports) 30 + 28 31 strongest = max(strongest, strength) 29 32 longest = max(longest, strength) 30 33 ··· 37 40 38 41 BRIDGES = [] 39 42 BRIDGE_PORTS = defaultdict(list) 43 + BRIDGE_EXTRAS = [] # tracks components of type `X/X` 40 44 41 - for i, line in enumerate(fileinput.input()): 42 - x, y = [int(x) for x in line.split('/')] 45 + for line in fileinput.input(): 46 + x, y = (int(x) for x in line.split('/')) 43 47 44 - BRIDGES.append((x, y)) 45 - BRIDGE_PORTS[x].append(i) 46 - BRIDGE_PORTS[y].append(i) 48 + if x != y: 49 + i = len(BRIDGES) 50 + BRIDGES.append((x, y)) 51 + BRIDGE_PORTS[x].append(i) 52 + BRIDGE_PORTS[y].append(i) 53 + else: 54 + BRIDGE_EXTRAS.append(x) 47 55 48 56 for i in BRIDGE_PORTS[0]: 49 57 strongest, longest = bridge_bfs(i, 0)