Advertisement
hhoppe

Advent of code 2018 day 23

Dec 15th, 2021 (edited)
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. def process1(s, part2=False):
  2.   # Divide-and-conquer using octree decomposition, adapted from
  3.   # https://github.com/wimglenn/advent-of-code-wim/blob/master/aoc_wim/aoc2018/q23.py.
  4.   # Improved to be robust (not assuming cubes with power-of-two dimensions).
  5.   pattern = r'pos=<([0-9-]+),([0-9-]+),([0-9-]+)>, r=(\d+)'
  6.   data = np.array([list(map(int, re.fullmatch(pattern, line).groups()))
  7.                    for line in s.strip('\n').split('\n')])
  8.   xs, rs = data[:, :-1], data[:, -1]
  9.   if not part2:
  10.     i = rs.argmax()
  11.     return (abs(xs - xs[i]).sum(axis=1) <= rs[i]).sum()
  12.  
  13.   xl, xh = xs.min(axis=0), xs.max(axis=0)
  14.   pq = [(0, (xh - xl).max(), abs(xl).sum(), tuple(xl), tuple(xh))]
  15.   while pq:
  16.     n_out, s, d, xl, xh = heapq.heappop(pq)
  17.     if s == 0:
  18.       return d
  19.     xm = (np.array(xl) + xh) // 2  # Partition into up to 8 octree child cells.
  20.     for child_min_max in itertools.product(
  21.         *(((l, m), (m + 1, h)) if m < h else ((l, h),)
  22.           for l, m, h in zip(xl, xm, xh))):
  23.       xl, xh = np.array(child_min_max).T
  24.       n_out = ((np.maximum(xl - xs, 0) + np.maximum(xs - xh, 0)).sum(axis=1)
  25.                > rs).sum()  # Maximize num in-range = minimize num out-of-range.
  26.       heapq.heappush(
  27.           pq, (n_out, (xh - xl).max(), abs(xl).sum(), tuple(xl), tuple(xh)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement