diff options
Diffstat (limited to 'aoc/datastructures.py')
| -rw-r--r-- | aoc/datastructures.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/aoc/datastructures.py b/aoc/datastructures.py new file mode 100644 index 0000000..c49f564 --- /dev/null +++ b/aoc/datastructures.py | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | from collections import namedtuple | ||
| 3 | |||
| 4 | |||
| 5 | class Coordinate(namedtuple("Coordinate", ["x", "y"])): | ||
| 6 | def __sub__(self, other: "Coordinate"): | ||
| 7 | return Coordinate(self.x - other.x, self.y - other.y) | ||
| 8 | |||
| 9 | def __add__(self, other: "Coordinate"): | ||
| 10 | return Coordinate(self.x + other.x, self.y + other.y) | ||
| 11 | |||
| 12 | def manhattan_distance(self, other: "Coordinate"): | ||
| 13 | return abs(self.x - other.x) + abs(self.y - other.y) | ||
| 14 | |||
| 15 | @property | ||
| 16 | def polarity_x(self): | ||
| 17 | try: | ||
| 18 | return abs(self.x) / self.x | ||
| 19 | except ZeroDivisionError: | ||
| 20 | return 0 | ||
| 21 | |||
| 22 | @property | ||
| 23 | def polarity_y(self): | ||
| 24 | try: | ||
| 25 | return abs(self.y) / self.y | ||
| 26 | except ZeroDivisionError: | ||
| 27 | return 0 | ||
