Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 22473db

Browse files
committed
Add day 18
1 parent cbaf615 commit 22473db

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

2024/18/18.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
import networkx as nx
22

33
def solve(obstacles, S=70):
4-
G = nx.DiGraph()
5-
for c in {x+1j*y for y in range(S+1) for x in range(S+1)}:
6-
for d in [1, 1j, -1, -1j]:
7-
if c not in obstacles and c+d not in obstacles:
8-
G.add_edge(c, c+d)
4+
G = nx.grid_graph((S+1, S+1))
5+
G.remove_nodes_from(obstacles)
6+
return nx.has_path(G, (0,0), (S,S)) \
7+
and len(nx.shortest_path(G, (0,0), (S,S)))
98

10-
return nx.has_path(G, 0, S+S*1j) and len(nx.shortest_path(G, 0, S+S*1j))
11-
12-
obstacles = [complex(line.replace(",", "+")+"j") for line in open(0)]
13-
print(solve(obstacles[:1024]))
9+
obstacles = [tuple(map(int, line.split(","))) for line in open(0)]
10+
print(solve(obstacles[:1024])-1)
1411

1512
lo, hi = 1024, len(obstacles)-1
1613
while lo < hi:
1714
mid = (lo + hi) // 2
1815
if solve(obstacles[:mid]): lo = mid+1
1916
else: hi = mid-1
2017

21-
print(str(obstacles[mid]).replace("+", ",")[1:-2])
18+
print(*obstacles[mid], sep=",")

0 commit comments

Comments
 (0)