Spaces:
Sleeping
Sleeping
File size: 1,905 Bytes
e067c2d 47bba68 e067c2d 47bba68 e067c2d 47bba68 e067c2d 47bba68 e067c2d 47bba68 e067c2d 47bba68 e067c2d 47bba68 e067c2d 47bba68 e067c2d |
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
"""Entity models for stores, destinations, tunnels, and trucks."""
from dataclasses import dataclass
from typing import Tuple
@dataclass
class Store:
"""Represents a storage location / starting point for trucks."""
id: int
position: Tuple[int, int]
def to_dict(self) -> dict:
return {
"id": self.id,
"position": {"x": self.position[0], "y": self.position[1]},
}
@dataclass
class Destination:
"""Represents a customer destination for package delivery."""
id: int
position: Tuple[int, int]
def to_dict(self) -> dict:
return {
"id": self.id,
"position": {"x": self.position[0], "y": self.position[1]},
}
@dataclass
class Tunnel:
"""Represents an underground tunnel connecting two points."""
entrance1: Tuple[int, int]
entrance2: Tuple[int, int]
@property
def cost(self) -> int:
"""Tunnel cost is Manhattan distance between entrances."""
return abs(self.entrance1[0] - self.entrance2[0]) + abs(
self.entrance1[1] - self.entrance2[1]
)
def get_other_entrance(self, entrance: Tuple[int, int]) -> Tuple[int, int]:
"""Get the other entrance of the tunnel."""
if entrance == self.entrance1:
return self.entrance2
elif entrance == self.entrance2:
return self.entrance1
raise ValueError(f"Position {entrance} is not an entrance of this tunnel")
def has_entrance_at(self, pos: Tuple[int, int]) -> bool:
"""Check if tunnel has an entrance at given position."""
return pos == self.entrance1 or pos == self.entrance2
def to_dict(self) -> dict:
return {
"entrance1": {"x": self.entrance1[0], "y": self.entrance1[1]},
"entrance2": {"x": self.entrance2[0], "y": self.entrance2[1]},
"cost": self.cost,
}
|