Guest User

Untitled

a guest
Dec 12th, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. def day12_1():
  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.     angle_to_drxn = {0: "E", 90: "N", 180: "W", 270: "S"}
  7.     totals = {"N": 0, "S": 0, "E": 0, "W": 0}
  8.     curr_angle = 0
  9.     for drxn, val in data:
  10.         if drxn == "L":
  11.             curr_angle = (curr_angle + val) % 360
  12.         elif drxn == "R":
  13.             curr_angle = (curr_angle - val) % 360
  14.         elif drxn == "F":
  15.             totals[angle_to_drxn[curr_angle]] += val
  16.         else:
  17.             totals[drxn] += val
  18.  
  19.     return abs(totals["N"] - totals["S"]) + abs(totals["E"] - totals["W"])
Advertisement
Add Comment
Please, Sign In to add comment