Grid() is now hashable

This commit is contained in:
Stefan Harmuth 2023-12-14 11:24:37 +01:00
parent f22c3bd798
commit 0408432e3d

View File

@ -1,11 +1,9 @@
from __future__ import annotations
import re
from collections import deque
from collections.abc import Callable
from .aoc_ocr import convert_array_6
from .coordinate import Coordinate, DistanceAlgorithm, Shape
from collections import deque
from collections.abc import Callable
from enum import Enum
from heapq import heappop, heappush
from math import inf
@ -219,17 +217,17 @@ class Grid:
def getActiveCells(
self, x: int = None, y: int = None, z: int = None
) -> List[Coordinate]:
) -> Iterable[Coordinate]:
if x is not None or y is not None or z is not None:
return [
return (
c
for c in self.__grid.keys()
if (c.x == x if x is not None else True)
and (c.y == y if y is not None else True)
and (c.z == z if z is not None else True)
]
)
else:
return list(self.__grid.keys())
return self.__grid.keys()
def getActiveRegion(
self,
@ -684,17 +682,5 @@ class Grid:
return grid
def __eq__(self, other: Grid) -> bool:
if not isinstance(other, Grid):
return False
other_active = set(other.getActiveCells())
for c, v in self.__grid.items():
if other.get(c) != v:
return False
other_active.remove(c)
if other_active:
return False
return True
def __hash__(self):
return hash(frozenset(self.__grid.items()))