Advertisement
Guest User

AOC 2020 - Day 12

a guest
Dec 17th, 2020
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.41 KB | None | 0 0
  1. # --- Day 12: Rain Risk ---
  2. with open('day12_input.txt') as f:
  3.     instructions = [[x[0], int(x[1:])] for x in f.read().splitlines()]
  4.  
  5. # Class for both parts.
  6. class Ship():
  7.     def __init__(self):
  8.         self.facing = 'E'
  9.         self.coord_x = 0
  10.         self.coord_y = 0
  11.         # Relatives to ship coords x y.
  12.         self.wayp_x = 10
  13.         self.wayp_y = 1
  14.  
  15.     def move(self, direction, number):
  16.         """
  17.        Moves the ship {number} units in {direction}
  18.        """
  19.         if direction == 'N':
  20.             self.coord_y += number
  21.         elif direction == 'S':
  22.             self.coord_y -= number
  23.         elif direction == 'E':
  24.             self.coord_x += number
  25.         elif direction == 'W':
  26.             self.coord_x -= number
  27.    
  28.     def move_waypoint(self, direction, number):
  29.         """
  30.        Moves the waypoint {number} units in {direction}
  31.        """
  32.         if direction == 'N':
  33.             self.wayp_y += number
  34.         elif direction == 'S':
  35.             self.wayp_y -= number
  36.         elif direction == 'E':
  37.             self.wayp_x += number
  38.         elif direction == 'W':
  39.             self.wayp_x -= number
  40.    
  41.     def rotate(self, turn, amount):
  42.         """
  43.        Rotates the ship in {turn} direction {amount} degrees
  44.        """
  45.         to_angles = {'N': 90, 'S': 270, 'E': 0, 'W': 180}
  46.         to_facing = {90: 'N', 270: 'S', 0: 'E', 180: 'W'}
  47.         if turn == 'R':
  48.             angle = (to_angles[self.facing] - amount) % 360
  49.             self.facing = to_facing[angle]
  50.         elif turn == 'L':
  51.             angle = (to_angles[self.facing] + amount) % 360
  52.             self.facing = to_facing[angle]
  53.    
  54.     def rotate_waypoint(self, turn, amount):
  55.         """
  56.        Rotates the waypoint around the ship
  57.        in {turn} direction {amount} degrees
  58.        """
  59.         if turn == 'R':
  60.             angle = 360 - amount
  61.         elif turn == 'L':
  62.             angle = amount
  63.  
  64.         if angle == 90:
  65.             helper = self.wayp_x
  66.             self.wayp_x = -1 * self.wayp_y
  67.             self.wayp_y = +1 * helper
  68.         elif angle == 180:
  69.             self.wayp_x = -1 * self.wayp_x
  70.             self.wayp_y = -1 * self.wayp_y
  71.         elif angle == 270:
  72.             helper = self.wayp_x
  73.             self.wayp_x = +1 * self.wayp_y
  74.             self.wayp_y = -1 * helper
  75.  
  76.     def move_ship_to_wayp(self, number):
  77.         self.coord_x += number * self.wayp_x
  78.         self.coord_y += number * self.wayp_y
  79.  
  80. # Part one.
  81. my_ship = Ship()
  82. for order in instructions:
  83.     action = order[0]
  84.     value = order[1]
  85.     if action == 'F':
  86.         my_ship.move(my_ship.facing, value)
  87.     elif action in ['R', 'L']:
  88.         my_ship.rotate(action, value)
  89.     elif action in ['N', 'S', 'E', 'W']:
  90.             my_ship.move(action, value)
  91.     #print(f"FACING: {my_ship.facing} - COORD X: {my_ship.coord_x} - COORD Y:{my_ship.coord_y}")
  92. answer_1 = abs(my_ship.coord_x) + abs(my_ship.coord_y)
  93. print(f"Part 1 - Manhattan Dist: {answer_1}")
  94.  
  95. # Part two.
  96. my_ship_2 = Ship()
  97. for order in instructions:
  98.     action = order[0]
  99.     value = order[1]
  100.     if action == 'F':
  101.         my_ship_2.move_ship_to_wayp(value)
  102.     elif action in ['R', 'L']:
  103.         my_ship_2.rotate_waypoint(action, value)
  104.     elif action in ['N', 'S', 'E', 'W']:
  105.             my_ship_2.move_waypoint(action, value)
  106. answer_2 = abs(my_ship_2.coord_x) + abs(my_ship_2.coord_y)
  107. print(f"Part 2 - Manhattan Dist: {answer_2}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement