summaryrefslogtreecommitdiffstats
path: root/aoc/datastructures.py
blob: 81a68e41afc64eff64604fe023ead8898c692c2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# -*- coding: utf-8 -*-
from collections import namedtuple


class Coordinate(namedtuple("Coordinate", ["x", "y"])):
    def __sub__(self, other: "Coordinate") -> "Coordinate":
        return Coordinate(self.x - other.x, self.y - other.y)

    def __add__(self, other: "Coordinate") -> "Coordinate":
        return Coordinate(self.x + other.x, self.y + other.y)

    def manhattan_distance(self, other: "Coordinate") -> int:
        return abs(self.x - other.x) + abs(self.y - other.y)

    @property
    def polarity(self) -> "Coordinate":
        try:
            px = abs(self.x) / self.x
        except ZeroDivisionError:
            px = 0

        try:
            py = abs(self.y) / self.y
        except ZeroDivisionError:
            py = 0

        return Coordinate(
            px,
            py,
        )