Advertisement
Guest User

Untitled

a guest
Dec 18th, 2023
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | Source Code | 0 0
  1. path = "day_18.txt"
  2. # path = "test.txt"
  3.  
  4. map_d = ['R', 'D', 'L', 'U']
  5.  
  6. part2 = True
  7.  
  8. with open(path, 'r') as file:
  9.     lines = [line.strip() for line in file]
  10.  
  11. def parse_line(l):
  12.     d, n, color = l.split()
  13.     if part2:
  14.         d = map_d[int(color[-2:-1])]
  15.         n = int('0'+color[2:-2], 16)
  16.     n = int(n)
  17.  
  18.     return n if d in 'RD' else -n
  19.  
  20. x = 0
  21. y = 0
  22. area = 0
  23. perimeter = 0
  24. for horizontal, vertical in zip(lines[::2], lines[1::2]):
  25.     x_n = parse_line(horizontal)
  26.     y_n = parse_line(vertical)
  27.     x += x_n
  28.     y += y_n
  29.     area += x*y_n
  30.     perimeter += abs(x_n) + abs(y_n)
  31.    
  32. print(abs(area) + perimeter//2 + 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement