-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommon.py
More file actions
37 lines (26 loc) · 886 Bytes
/
Copy pathcommon.py
File metadata and controls
37 lines (26 loc) · 886 Bytes
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
from typing import List, Tuple
class Instruction:
action: str
magnitude: int
raw: str
def __init__(self, instruction: str) -> None:
self.action = instruction[0].lower()
self.magnitude = int(instruction[1:])
self.raw = instruction
def parse(instr: str) -> List[Instruction]:
return [Instruction(x) for x in instr.strip().split("\n")]
def calculate_direction_deltas(direction: str, amount: int) -> Tuple[int, int]:
# returns a pair of deltas representing lat,long
lat_delta = 0
long_delta = 0
if direction == "n":
lat_delta += amount
elif direction == "s":
lat_delta -= amount
elif direction == "e":
long_delta += amount
elif direction == "w":
long_delta -= amount
else:
raise AssertionError(f"invalid direction '{direction}'")
return lat_delta, long_delta