Skip to content

Instantly share code, notes, and snippets.

@halitanildonmez
Created December 11, 2022 16:00
Show Gist options
  • Save halitanildonmez/fcb134d4a7852d6be0b6ea9cb99bd991 to your computer and use it in GitHub Desktop.
Save halitanildonmez/fcb134d4a7852d6be0b6ea9cb99bd991 to your computer and use it in GitHub Desktop.
Advent of Code 2020 day 12 pt 2
import math
T = "bb.txt"
M = "aa.txt"
file1 = open(M, 'r')
Lines = file1.read().splitlines()
sx, sy = 0, 0
wx, wy = 10, 1
input_map = {}
for i, line in enumerate(Lines):
if len(line.strip()) == 0:
continue
input_map[i] = line
for l in input_map:
order = input_map[l]
o, v = order[0], order[1:]
if o == 'F':
sx += wx * int(v)
sy += wy * int(v)
elif o == 'N':
wy += int(v)
elif o == 'S':
wy -= int(v)
elif o == 'E':
wx += int(v)
elif o == 'W':
wx -= int(v)
elif o == 'L':
# 2D rot formula
rad = math.radians(int(v))
xn = (math.cos(rad)*wx) - (math.sin(rad)*wy)
yn = (math.sin(rad)*wx) + (math.cos(rad)*wy)
wx = round(xn)
wy = round(yn)
elif o == 'R':
rad = math.radians(360 - int(v))
xn = (math.cos(rad)*wx) - (math.sin(rad)*wy)
yn = (math.sin(rad)*wx) + (math.cos(rad)*wy)
wx = round(xn)
wy = round(yn)
res = abs(sx) + abs(sy)
print(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment