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 2017/24

+110
+51
2017/day24.py
··· 1 + import fileinput 2 + from collections import defaultdict, deque 3 + 4 + 5 + def bridge_bfs(i, port): 6 + def bridge_strength(ports): 7 + """Returns the strength of a bridge given its ports.""" 8 + return sum(ports) * 2 - ports[0] - ports[-1] 9 + 10 + # (port_index, port_list, seen_indices) 11 + horizon = deque([(i, [port], set([i]))]) 12 + 13 + strongest = 0 14 + 15 + while horizon: 16 + longest = 0 17 + 18 + for _ in range(len(horizon)): 19 + i, ports, seen = horizon.popleft() 20 + seen.add(i) 21 + 22 + bridge = BRIDGES[i] 23 + port = bridge[0] if bridge[1] == ports[-1] else bridge[1] 24 + 25 + next_ports = ports + [port] 26 + strength = bridge_strength(next_ports) 27 + 28 + strongest = max(strongest, strength) 29 + longest = max(longest, strength) 30 + 31 + for p in BRIDGE_PORTS[port]: 32 + if p not in seen: 33 + horizon.append((p, list(next_ports), set(seen))) 34 + 35 + return strongest, longest 36 + 37 + 38 + BRIDGES = [] 39 + BRIDGE_PORTS = defaultdict(list) 40 + 41 + for i, line in enumerate(fileinput.input()): 42 + x, y = [int(x) for x in line.split('/')] 43 + 44 + BRIDGES.append((x, y)) 45 + BRIDGE_PORTS[x].append(i) 46 + BRIDGE_PORTS[y].append(i) 47 + 48 + for i in BRIDGE_PORTS[0]: 49 + strongest, longest = bridge_bfs(i, 0) 50 + print "Strength of strongest bridge:", strongest 51 + print "Strength of longest bridge:", longest
+57
2017/inputs/24.txt
··· 1 + 14/42 2 + 2/3 3 + 6/44 4 + 4/10 5 + 23/49 6 + 35/39 7 + 46/46 8 + 5/29 9 + 13/20 10 + 33/9 11 + 24/50 12 + 0/30 13 + 9/10 14 + 41/44 15 + 35/50 16 + 44/50 17 + 5/11 18 + 21/24 19 + 7/39 20 + 46/31 21 + 38/38 22 + 22/26 23 + 8/9 24 + 16/4 25 + 23/39 26 + 26/5 27 + 40/40 28 + 29/29 29 + 5/20 30 + 3/32 31 + 42/11 32 + 16/14 33 + 27/49 34 + 36/20 35 + 18/39 36 + 49/41 37 + 16/6 38 + 24/46 39 + 44/48 40 + 36/4 41 + 6/6 42 + 13/6 43 + 42/12 44 + 29/41 45 + 39/39 46 + 9/3 47 + 30/2 48 + 25/20 49 + 15/6 50 + 15/23 51 + 28/40 52 + 8/7 53 + 26/23 54 + 48/10 55 + 28/28 56 + 2/13 57 + 48/14
+2
2017/outputs/24.txt
··· 1 + 1695 2 + 1673