-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAOC_Day12_Part2.py
45 lines (39 loc) · 1.42 KB
/
AOC_Day12_Part2.py
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
38
39
40
41
42
43
44
45
import copy
import math
File = open("Day12_Input.txt", "r")
Lines = File.readlines()
PositionX = 0
PositionY = 0
WayPointX = 10
WayPointY = 1
Instruction = ["N","E","S","W"]
DirectionX = [0, 1, 0, -1]
DirectionY = [1, 0, -1, 0]
#Only L and R change the heading
for Line in Lines:
Step = int(Line[1:])
print(Line)
print(Step)
if Line[0] == "L":
Angle = int(Line[1:])*math.pi/180
print(Angle, math.cos(Angle), math.sin(Angle))
NewWayPointX = (WayPointX) * (math.cos(Angle)) - (WayPointY) * math.sin(Angle)
NewWayPointY = (WayPointX) * (math.sin(Angle)) + (WayPointY) * math.cos(Angle)
WayPointX = round(NewWayPointX)
WayPointY = round(NewWayPointY)
elif Line[0] == "R":
Angle = -int(Line[1:]) * math.pi / 180
NewWayPointX = ( WayPointX) * (math.cos(Angle)) - ( WayPointY) * math.sin(Angle)
NewWayPointY = ( WayPointX) * (math.sin(Angle)) + ( WayPointY) * math.cos(Angle)
WayPointX = round(NewWayPointX)
WayPointY = round(NewWayPointY)
elif Line[0] == "F":
PositionX += WayPointX * Step
PositionY += WayPointY * Step
else:
Index = Instruction.index(Line[0])
WayPointX += DirectionX[Index]*Step
WayPointY += DirectionY[Index]*Step
print("pos",PositionX,PositionY)
print("way",WayPointX,WayPointY)
print(abs(PositionX)+abs(PositionY))