-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
42 lines (30 loc) · 1.16 KB
/
Copy pathutil.py
File metadata and controls
42 lines (30 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from typing import Generic, TypeVar
HV_VARIANTS = ((1, 0), (0, 1), (-1, 0), (0, -1))
HZD_VARIANTS = ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1))
T = TypeVar("T")
class BaseBoard(Generic[T]):
func = lambda x: x
def __init__(self, input, func=None):
func = func or self.__class__.func
self.board: list[list[T]] = [[func(i) for i in row] for row in input]
self.height = len(self.board)
self.width = len(self.board[0])
def __str__(self):
return "\n".join("".join(str(e) for e in row) for row in self.board)
def __repr__(self):
return str(self)
def __eq__(self, other):
return self.board == other.board
def __ne__(self, other):
return not self.__eq__(other)
def get(self, x: int, y: int, default=None) -> T | None:
return (
self.board[y][x]
if 0 <= x < self.width and 0 <= y < self.height
else default
)
def find(self, e: T, default=None) -> tuple[int, int]:
for y, row in enumerate(self.board):
for x, c in enumerate(row):
if c == e:
return x, y