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