-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathmisc.py
More file actions
80 lines (65 loc) · 2.58 KB
/
misc.py
File metadata and controls
80 lines (65 loc) · 2.58 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from typing import List, Tuple, Union, Dict
class Slope(object):
__slots__ = ('rise', 'run')
def __init__(self, rise: int, run: int):
self.rise = rise
self.run = run
class Point(object):
__slots__ = ('x', 'y')
def __init__(self, x: int, y: int):
self.x = x
self.y = y
def copy(self) -> 'Point':
x = self.x
y = self.y
return Point(x, y)
def to_dict(self) -> Dict[str, int]:
d = {}
d['x'] = self.x
d['y'] = self.y
return d
@classmethod
def from_dict(cls, d: Dict[str, int]) -> 'Point':
return Point(d['x'], d['y'])
def __eq__(self, other: Union['Point', Tuple[int, int]]) -> bool:
if isinstance(other, tuple) and len(other) == 2:
return other[0] == self.x and other[1] == self.y
elif isinstance(other, Point) and self.x == other.x and self.y == other.y:
return True
return False
def __sub__(self, other: Union['Point', Tuple[int, int]]) -> 'Point':
if isinstance(other, tuple) and len(other) == 2:
diff_x = self.x - other[0]
diff_y = self.y - other[1]
return Point(diff_x, diff_y)
elif isinstance(other, Point):
diff_x = self.x - other.x
diff_y = self.y - other.y
return Point(diff_x, diff_y)
return None
def __rsub__(self, other: Tuple[int, int]):
diff_x = other[0] - self.x
diff_y = other[1] - self.y
return Point(diff_x, diff_y)
def __hash__(self) -> int:
return hash((self.x, self.y))
def __str__(self) -> str:
return '({}, {})'.format(self.x, self.y)
### These lines are defined such that facing "up" would be L0 ###
# Create 16 lines to be able to "see" around
VISION_16 = (
# L0 L1 L2 L3
Slope(-1, 0), Slope(-2, 1), Slope(-1, 1), Slope(-1, 2),
# L4 L5 L6 L7
Slope(0, 1), Slope(1, 2), Slope(1, 1), Slope(2, 1),
# L8 L9 L10 L11
Slope(1, 0), Slope(2, -1), Slope(1, -1), Slope(1, -2),
# L12 L13 L14 L15
Slope(0, -1), Slope(-1, -2), Slope(-1, -1), Slope(-2, -1)
)
# Create 8 lines to be able to "see" around
# Really just VISION_16 without odd numbered lines
VISION_8 = tuple([VISION_16[i] for i in range(len(VISION_16)) if i%2==0])
# Create 4 lines to be able to "see" around
# Really just VISION_16 but removing anything not divisible by 4
VISION_4 = tuple([VISION_16[i] for i in range(len(VISION_16)) if i%4==0])