Skip to content

Instantly share code, notes, and snippets.

@panki
Created May 26, 2022 06:11
Show Gist options
  • Save panki/a14ab17aa9c346a62bd9fe136e51d5c4 to your computer and use it in GitHub Desktop.
Save panki/a14ab17aa9c346a62bd9fe136e51d5c4 to your computer and use it in GitHub Desktop.
from itertools import product
from typing import List, Tuple
def __find_neighbours(x: int, y: int, size: int) -> List[Tuple]:
"""
Finds neighbours for a cell. Those which fall out of the field are skipped
:param x: current cell's X coord
:param y: current cell's Y coord
:param size: field single dimension size (because field is square)
:return: list of (x, y) pairs of valid neighbours
"""
return [
(x + dx, y + dy) for dx, dy in product([-1,0,1], repeat=2)
if (dx or dy) and 0 <= x + dx < size and 0 <= y + dy < size
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment