Advertisement
Guest User

AOC day 12 part 1 AND 2

a guest
Dec 12th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1.  
  2. # I love <3 complex numbers :3
  3.  
  4. import math
  5.  
  6. commands = [ [x[0],int(x[1:])] for x in open('input').read().splitlines()]
  7.  
  8. def pvec(d, m):
  9.     return {
  10.         'N' : complex(0,m),
  11.         'E' : complex(m,0),
  12.         'S' : complex(0,-m),
  13.         'W' : complex(-m,0)
  14.     }[d]
  15.  
  16. def a(commands):
  17.     fv = 0+0j
  18.     phase = 0
  19.     for v in commands:
  20.         d = v[0]
  21.         m = v[1]
  22.         if d == 'F':
  23.             fv += (m*(1j)**(phase/90))
  24.         elif d in ['L', 'R']:
  25.             phase += {
  26.                 'L' : m,
  27.                 'R' : -m
  28.             }[d]
  29.         else:
  30.             fv += pvec(d, m)
  31.     return (int)(abs(fv.real) + abs(fv.imag))
  32.  
  33. print(a(commands))
  34.  
  35. def b(commands):
  36.     fv = 0+0j
  37.     wv = 10+1j
  38.     phase = 0
  39.     for v in commands:
  40.         d = v[0]
  41.         m = v[1]
  42.         if d == 'F':
  43.             fv += m*wv
  44.         elif d in ['L', 'R']:
  45.             wv *= {
  46.                 'L' : (1j)**(m/90),
  47.                 'R' : (-1j)**(m/90)
  48.             }[d]
  49.         else:
  50.             wv += pvec(d, m)
  51.     return (int)(abs(fv.real) + abs(fv.imag))
  52.  
  53. print(b(commands))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement