Compare commits
3 Commits
b2cc1e814c
...
ffcc1e14c0
| Author | SHA1 | Date | |
|---|---|---|---|
| ffcc1e14c0 | |||
| 1a111488ba | |||
| 02623f8c9a |
@ -236,7 +236,7 @@ class Coordinate(tuple):
|
|||||||
diff = target - self
|
diff = target - self
|
||||||
|
|
||||||
if self[2] is None:
|
if self[2] is None:
|
||||||
steps = gcd(diff[0], diff[0])
|
steps = gcd(diff[0], diff[1])
|
||||||
step_x = diff[0] // steps
|
step_x = diff[0] // steps
|
||||||
step_y = diff[1] // steps
|
step_y = diff[1] // steps
|
||||||
return [
|
return [
|
||||||
|
|||||||
@ -229,6 +229,20 @@ class Grid:
|
|||||||
else:
|
else:
|
||||||
return self.__grid.keys()
|
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(
|
def getActiveRegion(
|
||||||
self,
|
self,
|
||||||
start: Coordinate,
|
start: Coordinate,
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import os.path
|
|||||||
import sys
|
import sys
|
||||||
from fishhook import hook
|
from fishhook import hook
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from typing import Any
|
from typing import Any, Iterator
|
||||||
|
|
||||||
|
|
||||||
class Cache(dict):
|
class Cache(dict):
|
||||||
@ -166,6 +166,18 @@ def cache(func):
|
|||||||
return new_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)
|
@hook(list)
|
||||||
def intersection(self, *args) -> list:
|
def intersection(self, *args) -> list:
|
||||||
ret = set(self).intersection(*args)
|
ret = set(self).intersection(*args)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user