Skip to content

Commit

Permalink
add ZGrid.zball convenience
Browse files Browse the repository at this point in the history
  • Loading branch information
wimglenn committed Jan 4, 2025
1 parent 3849994 commit 60e5059
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
3 changes: 1 addition & 2 deletions aoc_wim/aoc2024/q20.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from aocd import data
from aocd import extra

from aoc_wim.zgrid import manhattan_ball
from aoc_wim.zgrid import ZGrid


Expand All @@ -28,7 +27,7 @@ def n_cheats(t_cheat, dt_min):
result = 0
for z0, d in path.items():
for r in range(1, t_cheat + 1):
for z in manhattan_ball(r, z0):
for z in grid.zball(z0, r):
if z in path:
result += d - r - path[z] >= dt_min
return result
Expand Down
5 changes: 4 additions & 1 deletion aoc_wim/zgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def manhattan_distance(z1, z0=0):
return abs(int(dz.real)) + abs(int(dz.imag))


def manhattan_ball(r=1, z0=0, full=False):
def manhattan_ball(z0=0, r=1, full=False):
"""circle/ball centered on z0 with radius r (using manhattan distance)"""
result = []
if full:
result.append(0)
Expand Down Expand Up @@ -177,6 +178,8 @@ def remove(self, val):
self.d.update(keep)
return n_removed

zball = staticmethod(manhattan_ball)

@staticmethod
def near(z, n=4):
if n == 4:
Expand Down

0 comments on commit 60e5059

Please sign in to comment.