Skip to content

Commit fba11fa

Browse files
committed
Add day 16
1 parent 7554f59 commit fba11fa

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

2024/16/16.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,16 @@
22
coords = {x+1j*y: c for y, r in enumerate(open(0)) for x, c in enumerate(r.strip())}
33

44
G = nx.DiGraph()
5-
65
for c in coords:
7-
if coords[c] == '#': continue
86
for d in [1, 1j, -1, -1j]:
9-
G.add_edge((c, d), c, weight=0)
10-
G.add_edge(c, (c, d), weight=1000)
11-
if coords[c+d] != '#':
7+
G.add_edge((c, d), (c, 0), weight=0)
8+
G.add_edge((c, 0), (c, d), weight=1000)
9+
if coords[c] != '#' != coords[c+d]:
1210
G.add_edge((c, d), (c+d, d), weight=1)
1311

14-
S = [c for c in coords if coords[c] == 'S'][0]
15-
E = [c for c in coords if coords[c] == 'E'][0]
12+
E, S = [c for c in coords if coords[c] in 'SE']
1613

17-
all_paths = list(nx.all_shortest_paths(G, (S, 1), E, "weight"))
18-
path = all_paths[0]
19-
print(sum(G.edges[edge]["weight"] for edge in zip(path, path[1:])))
14+
paths = list(nx.all_shortest_paths(G, (S, 1), (E, 0), "weight"))
2015

21-
flat = sum(all_paths, [])
22-
nodes = {(p[0] if isinstance(p, tuple) else p) for p in flat}
23-
print(len(nodes))
16+
print(sum(G.edges[e]["weight"] for e in zip(paths[0], paths[0][1:])))
17+
print(len({p[0] for p in sum(paths, [])}))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy