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 solution for 2023/23

Store seen state as a bitfield instead of a set when computing the
longest path to improve runtime by around 30%.

+8 -3
+8 -3
2023/day23.py
··· 39 39 40 40 41 41 def longest_path(graph, start, end, part_2=False): 42 - horizon = [(start, 0, set())] 42 + BITMASK_MAPPING = {k: i for i, k in enumerate(graph)} 43 + horizon = [(start, 0, 0b0)] 43 44 best = 0 44 45 45 46 while horizon: ··· 49 50 best = max(best, dist) 50 51 continue 51 52 52 - if curr in seen: 53 + mask = (1 << BITMASK_MAPPING[curr]) 54 + 55 + if seen & mask: 53 56 continue 57 + 58 + new_seen = seen | mask 54 59 55 60 for neighbour, weight in graph[curr]: 56 - horizon.append((neighbour, dist + weight, seen | set([curr]))) 61 + horizon.append((neighbour, dist + weight, new_seen)) 57 62 58 63 return best 59 64