Skip to content

Commit d19fc0a

Browse files
committed
Fix bug and remove id function
1 parent d2d307b commit d19fc0a

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

2024/day16/solution.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,11 @@
1515
end = p
1616
grid[p] = "."
1717

18-
19-
def make_id():
20-
return str(uuid.uuid4().fields[-1])[:20]
21-
22-
2318
scores = defaultdict(lambda: float("inf"))
24-
previous = defaultdict(set)
19+
previous = {}
2520
best_score = float("inf")
2621

27-
q = [(0, make_id(), start, 1)]
22+
q = [(0, i := 0, start, 1)]
2823
while q:
2924
score, _, p, dp = heappop(q)
3025
if score > best_score:
@@ -39,10 +34,12 @@ def make_id():
3934
new_dps.append(dp)
4035
costs.append(1)
4136
for new_p, new_dp, cost in zip(new_ps, new_dps, costs):
42-
if score + cost <= scores[(new_p, new_dp)]:
37+
if score + cost < scores[(new_p, new_dp)]:
4338
scores[(new_p, new_dp)] = score + cost
44-
previous[(new_p, new_dp)] |= {(p, dp)}
45-
heappush(q, (score + cost, make_id(), new_p, new_dp))
39+
previous[(new_p, new_dp)] = {(p, dp)}
40+
heappush(q, (score + cost, i := i + 1, new_p, new_dp))
41+
if score + cost == scores[(new_p, new_dp)]:
42+
previous[(new_p, new_dp)].add((p, dp))
4643

4744
# Part 1
4845
print(best_score)

0 commit comments

Comments
 (0)