Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fba11fa

Browse filesBrowse files
committed
Add day 16
1 parent 7554f59 commit fba11fa
Copy full SHA for fba11fa

File tree

1 file changed

+7
-13
lines changed
Filter options

1 file changed

+7
-13
lines changed

‎2024/16/16.py

Copy file name to clipboardExpand all lines: 2024/16/16.py
+7-13Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.