diff options
| author | 2022-12-17 17:17:54 +0100 | |
|---|---|---|
| committer | 2022-12-17 17:17:54 +0100 | |
| commit | 5fd442c3bba6b4c63facb6bd89ecc3a9736e1c8d (patch) | |
| tree | 7bfa9fa0971d7311272ea6f234c223093e4e9615 /aoc/datastructures.py | |
| parent | 2d1263453d26cd2c6a64a3b6141ee0a12ddc0b11 (diff) | |
| download | 2022-5fd442c3bba6b4c63facb6bd89ecc3a9736e1c8d.tar.gz 2022-5fd442c3bba6b4c63facb6bd89ecc3a9736e1c8d.tar.bz2 2022-5fd442c3bba6b4c63facb6bd89ecc3a9736e1c8d.zip | |
Added some utilities
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 | ||
