Day 12: Black code formatting
This commit is contained in:
parent
be2a2e4184
commit
ab5d287bc0
3 changed files with 43 additions and 28 deletions
|
@ -6,7 +6,7 @@ class Instruction:
|
|||
magnitude: str
|
||||
raw: str
|
||||
|
||||
def __init__(self, instruction:str) -> None:
|
||||
def __init__(self, instruction: str) -> None:
|
||||
self.action = instruction[0].lower()
|
||||
self.magnitude = int(instruction[1:])
|
||||
self.raw = instruction
|
||||
|
@ -16,7 +16,7 @@ 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]:
|
||||
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
|
||||
|
@ -25,7 +25,7 @@ def calculate_direction_deltas(direction:str, amount:int) -> Tuple[int, int]:
|
|||
lat_delta += amount
|
||||
elif direction == "s":
|
||||
lat_delta -= amount
|
||||
|
||||
|
||||
elif direction == "e":
|
||||
long_delta += amount
|
||||
elif direction == "w":
|
||||
|
@ -35,4 +35,3 @@ def calculate_direction_deltas(direction:str, amount:int) -> Tuple[int, int]:
|
|||
raise AssertionError(f"invalid direction '{direction}'")
|
||||
|
||||
return lat_delta, long_delta
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ bearings_ltr = {
|
|||
}
|
||||
|
||||
|
||||
def rotate_direction(current:str, direction:str, amount:int) -> str:
|
||||
def rotate_direction(current: str, direction: str, amount: int) -> str:
|
||||
|
||||
assert direction in ["l", "r"], f"invalid rotate direction '{direction}'"
|
||||
|
||||
|
@ -37,18 +37,26 @@ def rotate_direction(current:str, direction:str, amount:int) -> str:
|
|||
return bearings_num[current_bearing]
|
||||
|
||||
|
||||
def translate_movement(current_direction:str, instruction:Instruction) -> Tuple[str, int, int]:
|
||||
def translate_movement(
|
||||
current_direction: str, instruction: Instruction
|
||||
) -> Tuple[str, int, int]:
|
||||
# Returns the new current direction and the lat/long delta
|
||||
|
||||
lat_delta = 0
|
||||
long_delta = 0
|
||||
|
||||
if instruction.action in ["l", "r"]:
|
||||
current_direction = rotate_direction(current_direction, instruction.action, instruction.magnitude)
|
||||
current_direction = rotate_direction(
|
||||
current_direction, instruction.action, instruction.magnitude
|
||||
)
|
||||
elif instruction.action == "f":
|
||||
lat_delta, long_delta = calculate_direction_deltas(current_direction, instruction.magnitude)
|
||||
lat_delta, long_delta = calculate_direction_deltas(
|
||||
current_direction, instruction.magnitude
|
||||
)
|
||||
elif instruction.action in ["n", "s", "e", "w"]:
|
||||
lat_delta, long_delta = calculate_direction_deltas(instruction.action, instruction.magnitude)
|
||||
lat_delta, long_delta = calculate_direction_deltas(
|
||||
instruction.action, instruction.magnitude
|
||||
)
|
||||
else:
|
||||
raise AssertionError(f"invalid action '{instruction.action}'")
|
||||
|
||||
|
@ -59,8 +67,8 @@ def partOne(instr: str) -> int:
|
|||
input_list = parse(instr)
|
||||
|
||||
current_direction = "e"
|
||||
lat = 0 # north/south, pos/neg
|
||||
long = 0 # west/east, pos/neg
|
||||
lat = 0 # north/south, pos/neg
|
||||
long = 0 # west/east, pos/neg
|
||||
|
||||
for instruction in input_list:
|
||||
current_direction, lad, lod = translate_movement(current_direction, instruction)
|
||||
|
@ -70,8 +78,8 @@ def partOne(instr: str) -> int:
|
|||
# Calculate manhattan distance
|
||||
# If either value is negative, make it positive
|
||||
if lat < 0:
|
||||
lat = lat + -2*lat
|
||||
lat = lat + -2 * lat
|
||||
if long < 0:
|
||||
long = long + -2*long
|
||||
long = long + -2 * long
|
||||
|
||||
return lat + long
|
||||
|
|
|
@ -3,21 +3,23 @@ from typing import Tuple
|
|||
from common import *
|
||||
|
||||
|
||||
def make_positive(n:int) -> int:
|
||||
return n if n >= 0 else n + -2*n
|
||||
def make_positive(n: int) -> int:
|
||||
return n if n >= 0 else n + -2 * n
|
||||
|
||||
|
||||
def make_negative(n:int) -> int:
|
||||
return n if n < 0 else n + -2*n
|
||||
def make_negative(n: int) -> int:
|
||||
return n if n < 0 else n + -2 * n
|
||||
|
||||
|
||||
def rotate_waypoint(current:Tuple[int, int], direction:str, amount:int) -> Tuple[int, int]:
|
||||
def rotate_waypoint(
|
||||
current: Tuple[int, int], direction: str, amount: int
|
||||
) -> Tuple[int, int]:
|
||||
|
||||
assert direction in ["l", "r"], f"invalid rotate direction '{direction}'"
|
||||
|
||||
lat_delta, long_delta = current
|
||||
|
||||
times = int(amount / 90) # number of times to rotate the waypoint by
|
||||
times = int(amount / 90) # number of times to rotate the waypoint by
|
||||
|
||||
# Determine the current quadrant
|
||||
quadrant = None
|
||||
|
@ -51,7 +53,7 @@ def rotate_waypoint(current:Tuple[int, int], direction:str, amount:int) -> Tuple
|
|||
t = lat_delta
|
||||
lat_delta = long_delta
|
||||
long_delta = t
|
||||
|
||||
|
||||
# Transform deltas into their new quadrant
|
||||
if new_quadrant == 1:
|
||||
lat_delta = make_positive(lat_delta)
|
||||
|
@ -69,23 +71,29 @@ def rotate_waypoint(current:Tuple[int, int], direction:str, amount:int) -> Tuple
|
|||
return lat_delta, long_delta
|
||||
|
||||
|
||||
def move_to_waypoint(waypoint_delta:Tuple[int, int], times:int) -> Tuple[int, int]:
|
||||
def move_to_waypoint(waypoint_delta: Tuple[int, int], times: int) -> Tuple[int, int]:
|
||||
lat_delta, long_delta = waypoint_delta
|
||||
return lat_delta * times, long_delta * times
|
||||
|
||||
|
||||
def translate_movement(waypoint_delta:Tuple[int, int], instruction:Instruction) -> Tuple[Tuple[int, int], int, int]:
|
||||
def translate_movement(
|
||||
waypoint_delta: Tuple[int, int], instruction: Instruction
|
||||
) -> Tuple[Tuple[int, int], int, int]:
|
||||
# Returns the new current direction and the lat/long delta
|
||||
|
||||
lat_delta = 0
|
||||
long_delta = 0
|
||||
|
||||
if instruction.action in ["l", "r"]:
|
||||
waypoint_delta = rotate_waypoint(waypoint_delta, instruction.action, instruction.magnitude)
|
||||
waypoint_delta = rotate_waypoint(
|
||||
waypoint_delta, instruction.action, instruction.magnitude
|
||||
)
|
||||
elif instruction.action == "f":
|
||||
lat_delta, long_delta = move_to_waypoint(waypoint_delta, instruction.magnitude)
|
||||
elif instruction.action in ["n", "s", "e", "w"]:
|
||||
wp_lat_delta, wp_long_delta = calculate_direction_deltas(instruction.action, instruction.magnitude)
|
||||
wp_lat_delta, wp_long_delta = calculate_direction_deltas(
|
||||
instruction.action, instruction.magnitude
|
||||
)
|
||||
wp_lat, wp_long = waypoint_delta
|
||||
waypoint_delta = (wp_lat + wp_lat_delta, wp_long + wp_long_delta)
|
||||
else:
|
||||
|
@ -98,8 +106,8 @@ def partTwo(instr: str) -> int:
|
|||
input_list = parse(instr)
|
||||
|
||||
waypoint_delta = (1, 10)
|
||||
lat = 0 # north/south, pos/neg
|
||||
long = 0 # west/east, pos/neg
|
||||
lat = 0 # north/south, pos/neg
|
||||
long = 0 # west/east, pos/neg
|
||||
|
||||
for instruction in input_list:
|
||||
waypoint_delta, lad, lod = translate_movement(waypoint_delta, instruction)
|
||||
|
@ -109,8 +117,8 @@ def partTwo(instr: str) -> int:
|
|||
# Calculate manhattan distance
|
||||
# If either value is negative, make it positive
|
||||
if lat < 0:
|
||||
lat = lat + -2*lat
|
||||
lat = lat + -2 * lat
|
||||
if long < 0:
|
||||
long = long + -2*long
|
||||
long = long + -2 * long
|
||||
|
||||
return lat + long
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue