summaryrefslogtreecommitdiffstats
path: root/aoc/datastructures.py
diff options
context:
space:
mode:
Diffstat (limited to 'aoc/datastructures.py')
-rw-r--r--aoc/datastructures.py23
1 files changed, 13 insertions, 10 deletions
diff --git a/aoc/datastructures.py b/aoc/datastructures.py
index c49f564..81a68e4 100644
--- a/aoc/datastructures.py
+++ b/aoc/datastructures.py
@@ -3,25 +3,28 @@ from collections import namedtuple
3 3
4 4
5class Coordinate(namedtuple("Coordinate", ["x", "y"])): 5class Coordinate(namedtuple("Coordinate", ["x", "y"])):
6 def __sub__(self, other: "Coordinate"): 6 def __sub__(self, other: "Coordinate") -> "Coordinate":
7 return Coordinate(self.x - other.x, self.y - other.y) 7 return Coordinate(self.x - other.x, self.y - other.y)
8 8
9 def __add__(self, other: "Coordinate"): 9 def __add__(self, other: "Coordinate") -> "Coordinate":
10 return Coordinate(self.x + other.x, self.y + other.y) 10 return Coordinate(self.x + other.x, self.y + other.y)
11 11
12 def manhattan_distance(self, other: "Coordinate"): 12 def manhattan_distance(self, other: "Coordinate") -> int:
13 return abs(self.x - other.x) + abs(self.y - other.y) 13 return abs(self.x - other.x) + abs(self.y - other.y)
14 14
15 @property 15 @property
16 def polarity_x(self): 16 def polarity(self) -> "Coordinate":
17 try: 17 try:
18 return abs(self.x) / self.x 18 px = abs(self.x) / self.x
19 except ZeroDivisionError: 19 except ZeroDivisionError:
20 return 0 20 px = 0
21 21
22 @property
23 def polarity_y(self):
24 try: 22 try:
25 return abs(self.y) / self.y 23 py = abs(self.y) / self.y
26 except ZeroDivisionError: 24 except ZeroDivisionError:
27 return 0 25 py = 0
26
27 return Coordinate(
28 px,
29 py,
30 )