Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import cos, sin, radians
- input = open('input12.txt', 'r')
- navinstructions = input.read().split('\n')
- pos = [0, 0]
- waypoint = [10, 1]
- positions = ['E', 'N', 'W', 'S']
- # x = 0
- # y = 0
- # def movein(direction, value, x, y):
- # if direction == 'F':
- # return movein(pos, value, x, y)
- # elif direction == 'E':
- # x += value
- # elif direction == 'W':
- # x -= value
- # elif direction == 'N':
- # y += value
- # elif direction == 'S':
- # y -= value
- # return x, y
- # def changedir(direction, value, pos, positions):
- # if direction == 'L':
- # return positions[(positions.index(pos) + int(value/90)) % 4]
- # elif direction == 'R':
- # return positions[(positions.index(pos) - int(value/90))]
- def wayin(direction, value, waypoint):
- x, y = waypoint[0], waypoint[1]
- if direction == 'E':
- x += value
- elif direction == 'W':
- x -= value
- elif direction == 'N':
- y += value
- elif direction == 'S':
- y -= value
- return [x, y]
- def changedir(direction, value, waypoint):
- x, y = waypoint[0], waypoint[1]
- if direction == 'L':
- return [x*cos(radians(value))-y*sin(radians(value)), x*sin(radians(value))+y*cos(radians(value))]
- elif direction == 'R':
- value = -value
- return [x*cos(radians(value))-y*sin(radians(value)), x*sin(radians(value))+y*cos(radians(value))]
- def movein(value, pos, waypoint):
- dirx, diry = waypoint[0], waypoint[1]
- x, y = pos[0], pos[1]
- x += dirx * value
- y += diry * value
- return [x, y]
- for navinstruction in navinstructions:
- instruction, value = navinstruction[0], int(navinstruction[1:])
- if instruction == 'F':
- pos = movein(value, pos, waypoint)
- elif instruction in positions:
- waypoint = wayin(instruction, value, waypoint)
- else:
- waypoint = changedir(instruction, value, waypoint)
- print(abs(pos[0])+abs(pos[1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement