From 06cb539f69f0b501afaa9ef5b6d89863e1c9d111 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Sat, 17 Dec 2022 17:25:46 +0100 Subject: Added some utilities --- aoc/datastructures.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'aoc/datastructures.py') 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 class Coordinate(namedtuple("Coordinate", ["x", "y"])): - def __sub__(self, other: "Coordinate"): + def __sub__(self, other: "Coordinate") -> "Coordinate": return Coordinate(self.x - other.x, self.y - other.y) - def __add__(self, other: "Coordinate"): + def __add__(self, other: "Coordinate") -> "Coordinate": return Coordinate(self.x + other.x, self.y + other.y) - def manhattan_distance(self, other: "Coordinate"): + def manhattan_distance(self, other: "Coordinate") -> int: return abs(self.x - other.x) + abs(self.y - other.y) @property - def polarity_x(self): + def polarity(self) -> "Coordinate": try: - return abs(self.x) / self.x + px = abs(self.x) / self.x except ZeroDivisionError: - return 0 + px = 0 - @property - def polarity_y(self): try: - return abs(self.y) / self.y + py = abs(self.y) / self.y except ZeroDivisionError: - return 0 + py = 0 + + return Coordinate( + px, + py, + ) -- cgit v1.2.3