hhoppe

Advent of code 2021 day 22 most compact

Dec 27th, 2021 (edited)
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. def process1(s, part2=False):
  2.   # Approach inspired from https://www.youtube.com/watch?v=YKpViLcTp64.
  3.   lines = s.strip('\n').split('\n')
  4.   states = [l[:3].strip() == 'on' for l in lines]
  5.   boxes = np.array([[list(map(int, range_[2:].split('..')))
  6.                     for range_ in l[3:].strip().split(',')] for l in lines])
  7.   if not part2:
  8.     boxes = boxes[((boxes[..., 0] >= -50) & (boxes[..., 1] <= 50)).all(axis=1)]
  9.   boxes[..., 1] += 1
  10.   coords, inverses = zip(*(np.unique(boxes[:, c], return_inverse=True)
  11.                            for c in range(boxes.shape[1])))
  12.   is_on = np.full([len(coord) - 1 for coord in coords], False)
  13.   for state, *ranges in zip(states, *(a.reshape(-1, 2) for a in inverses)):
  14.     is_on[tuple(slice(start, stop) for start, stop in ranges)] = state
  15.   return np.sum(is_on * functools.reduce(
  16.       np.multiply.outer, [np.diff(coord) for coord in coords]))
  17.  
Add Comment
Please, Sign In to add comment