summaryrefslogtreecommitdiffstats
path: root/aoc/datastructures.py
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2022-12-17 17:25:46 +0100
committerGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2022-12-17 17:25:46 +0100
commit06cb539f69f0b501afaa9ef5b6d89863e1c9d111 (patch)
tree77046b3ba1bda36309718be34347e05008426ded /aoc/datastructures.py
parent5fd442c3bba6b4c63facb6bd89ecc3a9736e1c8d (diff)
download2022-06cb539f69f0b501afaa9ef5b6d89863e1c9d111.tar.gz
2022-06cb539f69f0b501afaa9ef5b6d89863e1c9d111.tar.bz2
2022-06cb539f69f0b501afaa9ef5b6d89863e1c9d111.zip
Added some utilities
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 )