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