Skip to content

Commit 51f1abf

Browse files
committed
Add solution to 2024-12-20
1 parent bf8c0f3 commit 51f1abf

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

2024/day20/solutions.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from itertools import combinations
2+
3+
import networkx as nx
4+
5+
with open("input") as f:
6+
ls = f.read().strip().split("\n")
7+
8+
G = nx.grid_2d_graph(len(ls), len(ls[0]))
9+
for i, l in enumerate(ls):
10+
for j, x in enumerate(l):
11+
p = (i, j)
12+
if x == "#":
13+
G.remove_node(p)
14+
elif x == "S":
15+
start = p
16+
17+
dist = nx.single_source_dijkstra_path_length(G, start)
18+
19+
20+
def solve(cheat_dist):
21+
return sum(
22+
(d := abs(p1 - q1) + abs(p2 - q2)) <= cheat_dist and d2 - d1 - d >= 100
23+
for ((p1, p2), d1), ((q1, q2), d2) in combinations(dist.items(), 2)
24+
)
25+
26+
27+
# Part 1
28+
print(solve(2))
29+
30+
# Part 2
31+
print(solve(20))

0 commit comments

Comments
 (0)