Last active
December 12, 2020 10:25
-
-
Save rendyanthony/8f4b807c7822aa7d8b59d500b24e1943 to your computer and use it in GitHub Desktop.
Advent of Code Day 12 (Part 2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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