Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize day 20 #32

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions aoc_2024/day_20/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {}
Expand Down
Loading