day23 better way to check moveable elves

This commit is contained in:
Ruediger Ludwig 2023-01-23 19:07:12 +01:00
parent 840e7abc4d
commit 867a476c44
2 changed files with 97 additions and 75 deletions

View file

@ -76,6 +76,16 @@ class Position:
yield self.left()
yield self.down()
def all_neighbors(self) -> Iterator[Position]:
yield Position(self.x + 1, self.y)
yield Position(self.x + 1, self.y - 1)
yield Position(self.x, self.y - 1)
yield Position(self.x - 1, self.y - 1)
yield Position(self.x - 1, self.y)
yield Position(self.x - 1, self.y + 1)
yield Position(self.x, self.y + 1)
yield Position(self.x + 1, self.y + 1)
def is_within(self, top_left: Position, bottom_right: Position) -> bool:
"""
Checks if this point is within the rectangle spanned by the given positions.