-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.py
More file actions
55 lines (45 loc) · 1.76 KB
/
table.py
File metadata and controls
55 lines (45 loc) · 1.76 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
43
44
45
46
47
48
49
50
51
52
53
54
55
# This file currently is not used anywhere
from typing import Callable, List, Tuple
Position = Tuple[int, int]
PointFunction = Callable[[int, int], Position]
class Table(list[list[int]]):
def __init__(self, width: int, height: int):
super().__init__()
self.width = width
self.height = height
for _y in range(self.height):
row = [0 for _x in range(self.width)]
self.append(row)
def mark_by_positions(self, positions: List[Position]) -> int:
mark_count = 0
for pos in positions:
if self.is_in_boundry(pos):
self.set_by_position(pos, 1)
return mark_count
def mark_by_functions(self, functions: List[PointFunction]) -> int:
mark_count = 0
finished = False
while not finished:
finished = True
for y, row in enumerate(self):
for x, value in enumerate(row):
for fn in functions:
if value == 1:
relative = fn(x, y)
if self.is_in_boundry(relative):
if self.get_by_position(relative) == 0:
finished = False
self.set_by_position(relative, 1)
mark_count += 1
return mark_count
def get_mark_count(self):
return sum(map(sum, self))
def get_by_position(self, pos: Position):
x, y = pos
return self[y][x]
def set_by_position(self, pos: Position, value: int):
x, y = pos
self[y][x] = value
def is_in_boundry(self, pos: Position) -> bool:
x, y = pos
return 0 <= x < self.width and 0 <= y < self.height