Advertisement
Guest User

Untitled

a guest
Dec 12th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. from math import cos, sin, radians
  2.  
  3. input = open('input12.txt', 'r')
  4.  
  5. navinstructions = input.read().split('\n')
  6.  
  7. pos = [0, 0]
  8.  
  9. waypoint = [10, 1]
  10.  
  11. positions = ['E', 'N', 'W', 'S']
  12.  
  13. # x = 0
  14. # y = 0
  15.  
  16. # def movein(direction, value, x, y):
  17. # if direction == 'F':
  18. # return movein(pos, value, x, y)
  19. # elif direction == 'E':
  20. # x += value
  21. # elif direction == 'W':
  22. # x -= value
  23. # elif direction == 'N':
  24. # y += value
  25. # elif direction == 'S':
  26. # y -= value
  27. # return x, y
  28.  
  29. # def changedir(direction, value, pos, positions):
  30. # if direction == 'L':
  31. # return positions[(positions.index(pos) + int(value/90)) % 4]
  32. # elif direction == 'R':
  33. # return positions[(positions.index(pos) - int(value/90))]
  34.  
  35.  
  36. def wayin(direction, value, waypoint):
  37. x, y = waypoint[0], waypoint[1]
  38. if direction == 'E':
  39. x += value
  40. elif direction == 'W':
  41. x -= value
  42. elif direction == 'N':
  43. y += value
  44. elif direction == 'S':
  45. y -= value
  46. return [x, y]
  47.  
  48. def changedir(direction, value, waypoint):
  49. x, y = waypoint[0], waypoint[1]
  50. if direction == 'L':
  51. return [x*cos(radians(value))-y*sin(radians(value)), x*sin(radians(value))+y*cos(radians(value))]
  52. elif direction == 'R':
  53. value = -value
  54. return [x*cos(radians(value))-y*sin(radians(value)), x*sin(radians(value))+y*cos(radians(value))]
  55.  
  56.  
  57. def movein(value, pos, waypoint):
  58. dirx, diry = waypoint[0], waypoint[1]
  59. x, y = pos[0], pos[1]
  60. x += dirx * value
  61. y += diry * value
  62. return [x, y]
  63.  
  64.  
  65.  
  66. for navinstruction in navinstructions:
  67. instruction, value = navinstruction[0], int(navinstruction[1:])
  68. if instruction == 'F':
  69. pos = movein(value, pos, waypoint)
  70. elif instruction in positions:
  71. waypoint = wayin(instruction, value, waypoint)
  72. else:
  73. waypoint = changedir(instruction, value, waypoint)
  74.  
  75.  
  76. print(abs(pos[0])+abs(pos[1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement