Skip to content

Commit

Permalink
Merge pull request #4 from baurt/add-point
Browse files Browse the repository at this point in the history
add point to grid.
  • Loading branch information
TillerBurr authored Sep 6, 2023
2 parents 5d5b36d + 035ef6e commit eb38380
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions project/src/{{package_name}}/shared/grid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from dataclasses import dataclass
from typing import Literal, Self


@dataclass(frozen=True)
class Point:
x: int
y: int

def move(self, other_x: int, other_y: int) -> Self:
return Point(self.x + other_x, self.y + other_y)

def __add__(self, other: Self) -> Self:
return Point(self.x + other.x, self.y + other.y)

def __sub__(self, other: Self) -> Self:
return Point(self.x - other.x, self.y - other.y)

def adjacent_dist(self, other: Self) -> int:
return max(abs(self.x - other.x), abs(self.y - other.y))

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

def dx(self, other: Self) -> int:
return self.x - other.x

def dy(self, other: Self) -> int:
return self.y - other.y

@property
def coords(self) -> tuple[int, int]:
return (self.x, self.y)

def get_adjacent(self) -> tuple[Self, Self, Self, Self]:
return (
Point(self.x + 1, self.y),
Point(self.x - 1, self.y),
Point(self.x, self.y + 1),
Point(self.x, self.y - 1),
)


@dataclass
class Movement:
direction: Literal["U"] | Literal["D"] | Literal["L"] | Literal["R"]
amount: int


DIRECTIONS = {"U": Point(0, 1), "D": Point(0, -1), "L": Point(-1, 0), "R": Point(1, 0)}

0 comments on commit eb38380

Please sign in to comment.