Advertisement
hhoppe

Advent of code 2023 day 18

Dec 18th, 2023 (edited)
896
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.47 KB | None | 0 0
  1. def day18(s, *, part2=False):
  2.   x = 0
  3.   area = 1
  4.   for line in s.splitlines():
  5.     op, s_num, color = re.match(r'^([LRUD]) (\d+) \(#(......)\)$', line).groups()
  6.     num = int(color[:5], 16) if part2 else int(s_num)
  7.     op = 'RDLU'[int(color[5])] if part2 else op
  8.     match op:
  9.       case 'R':
  10.         x += num
  11.         area += num
  12.       case 'L':
  13.         x -= num
  14.       case 'D':
  15.         area += x * num + num
  16.       case 'U':
  17.         area -= x * num
  18.  
  19.   return area
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement