Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize day 20
Browse files Browse the repository at this point in the history
tyler-hoffman committed Dec 20, 2024
1 parent c6ffe23 commit 254d8b2
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions aoc_2024/day_20/solver.py
Original file line number Diff line number Diff line change
@@ -18,19 +18,30 @@ def solution(self) -> int:
@cached_property
def cheats(self) -> Iterator[int]:
for p, p_dist in self.dists_to_end.items():
for y in range(p.y - self.max_cheat, p.y + self.max_cheat + 1):
for x in range(p.x - self.max_cheat, p.x + self.max_cheat + 1):
n = Point(x, y)
travelled = n.dist(p)
if travelled <= self.max_cheat and n in self.non_walls:
n_dist = self.dists_to_end[n]
yield p_dist - n_dist - travelled
for offset in self.offsets:
n = p.add(offset)
travelled = n.dist(p)
if travelled <= self.max_cheat and n in self.non_walls:
n_dist = self.dists_to_end[n]
yield p_dist - n_dist - travelled

def second_neighbors(self, p: Point) -> Iterator[Point]:
for a in p.neighbors:
for b in a.neighbors:
yield b

@cached_property
def offsets(self) -> set[Point]:
center = Point(0, 0)
output = set[Point]()
for y in range(-self.max_cheat, self.max_cheat + 1):
for x in range(-self.max_cheat, self.max_cheat + 1):
n = Point(x, y)
travelled = n.dist(center)
if travelled <= self.max_cheat:
output.add(n)
return output

@cached_property
def dists_to_end(self) -> dict[Point, int]:
output: dict[Point, int] = {}

0 comments on commit 254d8b2

Please sign in to comment.