-
Notifications
You must be signed in to change notification settings - Fork 1
/
routing.py
74 lines (67 loc) · 2.22 KB
/
routing.py
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
68
69
70
71
72
73
74
from datetime import datetime
from pytz import timezone
from stations import *
from enum import Enum
class Direction(Enum):
NORTH = 1
SOUTH = 2
EAST = 3
WEST = 4
def __str__(self):
return self.name
def __repr__(self):
return self.name
def findRoute(origin, dest):
return findRouteHelper(origin, dest, [])
def findRouteHelper(origin, dest, route):
origin_lines = findLinesWithStation(origin)
dest_lines = findLinesWithStation(dest)
intersects = origin_lines.intersection(dest_lines)
if len(intersects) >= 1:
route.append({
"lines": intersects,
"direction": findDirection(origin, dest, min(intersects)),
"destination": dest
})
return route
elif len(intersects) == 0:
dest_line_name = min(dest_lines).name
transfer = min(origin_lines).transfers[dest_line_name]
route.append({
"lines": origin_lines,
"direction": findDirection(origin, transfer, min(origin_lines)),
"destination": transfer
})
return findRouteHelper(transfer, dest, route)
def findLinesWithStation(station):
lines = set()
now_utc = datetime.now(timezone('UTC'))
now_eastern = now_utc.astimezone(timezone('US/Eastern'))
if now_eastern.hour <= 5 and now_eastern.hour >= 1 and now_eastern.minute >= 30:
return lines
if station in red.stations:
if now_eastern.hour >= 21 and now_eastern.hour <= 2:
lines.add(red_night)
else:
lines.add(red)
if station in gold.stations:
lines.add(gold)
if station in green.stations:
if now_eastern.hour >= 21 and now_eastern.hour <= 2:
lines.add(green_night)
else:
lines.add(green)
if station in blue.stations:
lines.add(blue)
return lines
def findDirection(origin, dest, line):
if line.name is 'red' or line.name is 'gold':
if line.stations.index(origin) < line.stations.index(dest):
return Direction.SOUTH
else:
return Direction.NORTH
else:
if line.stations.index(origin) < line.stations.index(dest):
return Direction.EAST
else:
return Direction.WEST