Guest User

Untitled

a guest
Dec 12th, 2020
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. def day12_2():
  2.     with open("input.txt", "r") as file:
  3.         data = file.readlines()
  4.     data = [(i[:1], int(i[1:-1])) for i in data]
  5.  
  6.     # (N/S, E/W) Positive = North, East. Negative = South, West
  7.     waypoint = [1, 10]
  8.     curr_pos = [0, 0]
  9.     delta_ns = {"N": 1, "S": -1, "E": 0, "W": 0}
  10.     delta_ew = {"N": 0, "S": 0, "E": 1, "W": -1}
  11.     for drxn, val in data:
  12.         if drxn == "F":
  13.             curr_pos = [curr_pos[0] + waypoint[0] * val, curr_pos[1] + waypoint[1] * val]
  14.         elif drxn == "L":
  15.             ccw = {90: [waypoint[1], -waypoint[0]], 180: [-waypoint[0], -waypoint[1]], 270: [-waypoint[1], waypoint[0]]}
  16.             waypoint = ccw[val]
  17.         elif drxn == "R":
  18.             cw = {90: [-waypoint[1], waypoint[0]], 180: [-waypoint[0], -waypoint[1]], 270: [waypoint[1], -waypoint[0]]}
  19.             waypoint = cw[val]
  20.         else:
  21.             waypoint = [waypoint[0] + delta_ns[drxn] * val, waypoint[1] + delta_ew[drxn] * val]
  22.  
  23.     return abs(curr_pos[0]) + abs(curr_pos[1])
Advertisement
Add Comment
Please, Sign In to add comment