Skip to content

Instantly share code, notes, and snippets.

@rendyanthony
Last active December 12, 2020 10:25
Show Gist options
  • Save rendyanthony/8f4b807c7822aa7d8b59d500b24e1943 to your computer and use it in GitHub Desktop.
Save rendyanthony/8f4b807c7822aa7d8b59d500b24e1943 to your computer and use it in GitHub Desktop.
Advent of Code Day 12 (Part 2)
COMPASS = {
"E": (+1, 0),
"S": (0, +1),
"W": (-1, 0),
"N": (0, -1)
}
ship_pos = [0, 0]
waypoint = [10, -1]
nav_list = [(n[0], int(n[1:])) for n in open("input_12")]
for nav, value in nav_list:
if nav in ("R", "L"):
if nav == "L":
value = -value
value %= 360
if value == 90:
waypoint = [-waypoint[1], waypoint[0]]
elif value == 180:
waypoint = [-waypoint[0], -waypoint[1]]
elif value == 270:
waypoint = [waypoint[1], -waypoint[0]]
if nav == "F":
ship_pos[0] += waypoint[0] * value
ship_pos[1] += waypoint[1] * value
if nav in COMPASS.keys():
waypoint[0] += COMPASS[nav][0] * value
waypoint[1] += COMPASS[nav][1] * value
print(abs(ship_pos[0]) + abs(ship_pos[1]))
COMPASS = {
"E": (+1, 0),
"S": (0, +1),
"W": (-1, 0),
"N": (0, -1)
}
ship_pos = [0, 0]
waypoint = [10, -1]
nav_list = [(n[0], int(n[1:])) for n in open("input_12")]
for nav, value in nav_list:
if nav in ("R", "L"):
value = (nav == "L") and -value or value
for q in range((value%360)//90):
waypoint = [-waypoint[1], waypoint[0]]
if nav == "F":
ship_pos[0] += waypoint[0] * value
ship_pos[1] += waypoint[1] * value
if nav in COMPASS.keys():
waypoint[0] += COMPASS[nav][0] * value
waypoint[1] += COMPASS[nav][1] * value
print(abs(ship_pos[0]) + abs(ship_pos[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment