Grid.find(): generator yielding coordinates that hold a specific value
This commit is contained in:
parent
cf72a5451f
commit
d6fad1511c
@ -173,6 +173,11 @@ class Grid:
|
|||||||
def getOnCount(self) -> int:
|
def getOnCount(self) -> int:
|
||||||
return len(self.__grid)
|
return len(self.__grid)
|
||||||
|
|
||||||
|
def find(self, value: Any) -> Iterable[Coordinate]:
|
||||||
|
for k, v in self.__grid.items():
|
||||||
|
if v == value:
|
||||||
|
yield k
|
||||||
|
|
||||||
def count(self, value: Any) -> int:
|
def count(self, value: Any) -> int:
|
||||||
return list(self.__grid.values()).count(value)
|
return list(self.__grid.values()).count(value)
|
||||||
|
|
||||||
@ -210,14 +215,9 @@ class Grid:
|
|||||||
and self.minZ + pad <= pos[2] <= self.maxZ - pad
|
and self.minZ + pad <= pos[2] <= self.maxZ - pad
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return (
|
return self.minX + pad <= pos[0] <= self.maxX - pad and self.minY + pad <= pos[1] <= self.maxY - pad
|
||||||
self.minX + pad <= pos[0] <= self.maxX - pad
|
|
||||||
and self.minY + pad <= pos[1] <= self.maxY - pad
|
|
||||||
)
|
|
||||||
|
|
||||||
def getActiveCells(
|
def getActiveCells(self, x: int = None, y: int = None, z: int = None) -> Iterable[Coordinate]:
|
||||||
self, x: int = None, y: int = None, z: int = None
|
|
||||||
) -> Iterable[Coordinate]:
|
|
||||||
if x is not None or y is not None or z is not None:
|
if x is not None or y is not None or z is not None:
|
||||||
return (
|
return (
|
||||||
c
|
c
|
||||||
@ -229,7 +229,7 @@ class Grid:
|
|||||||
else:
|
else:
|
||||||
return self.__grid.keys()
|
return self.__grid.keys()
|
||||||
|
|
||||||
def getRegion(self, start: Coordinate, includeDiagonal: bool = False) -> Iterable[Coordinate]:
|
def getRegion(self, start: Coordinate, includeDiagonal: bool = False) -> Iterable[Coordinate]:
|
||||||
start_value = self.get(start)
|
start_value = self.get(start)
|
||||||
queue = deque()
|
queue = deque()
|
||||||
queue.append(start)
|
queue.append(start)
|
||||||
@ -421,9 +421,7 @@ class Grid:
|
|||||||
if c in came_from and self.get(c) in walls:
|
if c in came_from and self.get(c) in walls:
|
||||||
continue
|
continue
|
||||||
came_from[c] = current
|
came_from[c] = current
|
||||||
if c == pos_to or (
|
if c == pos_to or (stop_at_first is not None and self.get(c) == stop_at_first):
|
||||||
stop_at_first is not None and self.get(c) == stop_at_first
|
|
||||||
):
|
|
||||||
pos_to = c
|
pos_to = c
|
||||||
found_end = True
|
found_end = True
|
||||||
break
|
break
|
||||||
@ -470,9 +468,7 @@ class Grid:
|
|||||||
if currentCoord == pos_to:
|
if currentCoord == pos_to:
|
||||||
break
|
break
|
||||||
|
|
||||||
for neighbour in self.getNeighboursOf(
|
for neighbour in self.getNeighboursOf(currentCoord, includeDefault=True, includeDiagonal=includeDiagonal):
|
||||||
currentCoord, includeDefault=True, includeDiagonal=includeDiagonal
|
|
||||||
):
|
|
||||||
if self.get(neighbour) in walls or neighbour in closedNodes:
|
if self.get(neighbour) in walls or neighbour in closedNodes:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@ -481,9 +477,7 @@ class Grid:
|
|||||||
elif not includeDiagonal:
|
elif not includeDiagonal:
|
||||||
neighbourDist = 1
|
neighbourDist = 1
|
||||||
else:
|
else:
|
||||||
neighbourDist = currentCoord.getDistanceTo(
|
neighbourDist = currentCoord.getDistanceTo(neighbour, DistanceAlgorithm.MANHATTAN, includeDiagonal)
|
||||||
neighbour, DistanceAlgorithm.MANHATTAN, includeDiagonal
|
|
||||||
)
|
|
||||||
|
|
||||||
targetDist = neighbour.getDistanceTo(pos_to)
|
targetDist = neighbour.getDistanceTo(pos_to)
|
||||||
f_cost = targetDist + neighbourDist + currentNode[1]
|
f_cost = targetDist + neighbourDist + currentNode[1]
|
||||||
@ -517,17 +511,13 @@ class Grid:
|
|||||||
to_z: int = None,
|
to_z: int = None,
|
||||||
) -> "Grid":
|
) -> "Grid":
|
||||||
if self.mode3D and (from_z is None or to_z is None):
|
if self.mode3D and (from_z is None or to_z is None):
|
||||||
raise ValueError(
|
raise ValueError("sub_grid() on mode3d Grids requires from_z and to_z to be set")
|
||||||
"sub_grid() on mode3d Grids requires from_z and to_z to be set"
|
|
||||||
)
|
|
||||||
count_x, count_y, count_z = 0, 0, 0
|
count_x, count_y, count_z = 0, 0, 0
|
||||||
new_grid = Grid(self.__default)
|
new_grid = Grid(self.__default)
|
||||||
for x in range(from_x, to_x + 1):
|
for x in range(from_x, to_x + 1):
|
||||||
for y in range(from_y, to_y + 1):
|
for y in range(from_y, to_y + 1):
|
||||||
if not self.mode3D:
|
if not self.mode3D:
|
||||||
new_grid.set(
|
new_grid.set(Coordinate(count_x, count_y), self.get(Coordinate(x, y)))
|
||||||
Coordinate(count_x, count_y), self.get(Coordinate(x, y))
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
for z in range(from_z, to_z + 1):
|
for z in range(from_z, to_z + 1):
|
||||||
new_grid.set(
|
new_grid.set(
|
||||||
@ -594,20 +584,14 @@ class Grid:
|
|||||||
def get_aoc_ocr_string(self, x_shift: int = 0, y_shift: int = 0):
|
def get_aoc_ocr_string(self, x_shift: int = 0, y_shift: int = 0):
|
||||||
return convert_array_6(
|
return convert_array_6(
|
||||||
[
|
[
|
||||||
[
|
["#" if self.get(Coordinate(x + x_shift, y + y_shift)) else "." for x in self.rangeX()]
|
||||||
"#" if self.get(Coordinate(x + x_shift, y + y_shift)) else "."
|
|
||||||
for x in self.rangeX()
|
|
||||||
]
|
|
||||||
for y in self.rangeY()
|
for y in self.rangeY()
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
def __str__(self, true_char: str = "#", false_char: str = "."):
|
def __str__(self, true_char: str = "#", false_char: str = "."):
|
||||||
return "/".join(
|
return "/".join(
|
||||||
"".join(
|
"".join(true_char if self.get(Coordinate(x, y)) else false_char for x in range(self.minX, self.maxX + 1))
|
||||||
true_char if self.get(Coordinate(x, y)) else false_char
|
|
||||||
for x in range(self.minX, self.maxX + 1)
|
|
||||||
)
|
|
||||||
for y in range(self.minY, self.maxY + 1)
|
for y in range(self.minY, self.maxY + 1)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -623,11 +607,7 @@ class Grid:
|
|||||||
) -> "Grid":
|
) -> "Grid":
|
||||||
if translate is None:
|
if translate is None:
|
||||||
translate = {}
|
translate = {}
|
||||||
if (
|
if true_char is not None and True not in translate.values() and true_char not in translate:
|
||||||
true_char is not None
|
|
||||||
and True not in translate.values()
|
|
||||||
and true_char not in translate
|
|
||||||
):
|
|
||||||
translate[true_char] = true_value if true_value is not None else True
|
translate[true_char] = true_value if true_value is not None else True
|
||||||
|
|
||||||
ret = cls(default=default)
|
ret = cls(default=default)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user