Compare commits

..

3 Commits

3 changed files with 28 additions and 2 deletions

View File

@ -236,7 +236,7 @@ class Coordinate(tuple):
diff = target - self
if self[2] is None:
steps = gcd(diff[0], diff[0])
steps = gcd(diff[0], diff[1])
step_x = diff[0] // steps
step_y = diff[1] // steps
return [

View File

@ -229,6 +229,20 @@ class Grid:
else:
return self.__grid.keys()
def getRegion(self, start: Coordinate, includeDiagonal: bool = False) -> Iterable[Coordinate]:
start_value = self.get(start)
queue = deque()
queue.append(start)
visited = set()
while queue:
next_coord = queue.popleft()
if next_coord in visited or not self.isWithinBoundaries(next_coord) or self.get(next_coord) != start_value:
continue
visited.add(next_coord)
yield next_coord
for n in self.getNeighboursOf(next_coord, includeDefault=True, includeDiagonal=includeDiagonal):
queue.append(n)
def getActiveRegion(
self,
start: Coordinate,

View File

@ -4,7 +4,7 @@ import os.path
import sys
from fishhook import hook
from functools import wraps
from typing import Any
from typing import Any, Iterator
class Cache(dict):
@ -166,6 +166,18 @@ def cache(func):
return new_func
def list_combinations_of_sum(total_sum: int, length: int = None, min_value: int = 0) -> Iterator[tuple[int]]:
if length is None:
length = total_sum
if length == 1:
yield (total_sum,)
else:
for value in range(min_value, total_sum + 1):
for permutation in list_combinations_of_sum(total_sum - value, length - 1, min_value):
yield (value,) + permutation
@hook(list)
def intersection(self, *args) -> list:
ret = set(self).intersection(*args)