Advertisement
icarussiano

day18

Dec 18th, 2023 (edited)
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. ll=open("input").read().splitlines()
  2. def pick(v,l):
  3.     x,y=zip(*v)
  4.     return int(0.5*abs(sum(x[i]*y[i-1]-x[i-1]*y[i] for i in range(len(v))))+0.5*l+1)
  5. for p1 in [True,False]:
  6.     dir= {"R":(1,0), "L":(-1,0), "U":(0,1), "D":(0,-1), "0":(1,0), "2":(-1,0), "1":(0,1), "3":(0,-1)}
  7.     x,y,l=0,0,0
  8.     v=[(x,y)]
  9.     for line in ll:
  10.         d, n = (line.split()[0],int(line.split()[1])) if p1 else (line.split()[2][-2], int(line.split()[2][2:-2],16))
  11.         x,y=x+dir[d][0]*int(n),y+dir[d][1]*int(n)
  12.         v.append((x,y))
  13.         l+=n
  14.     print(f"Part 1: {pick(v,l)}") if p1 else print(f"Part 2: {pick(v,l)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement