Advertisement
hhoppe

Advent of code 2022 day 23

Dec 23rd, 2022
957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. def day23(s, *, part2=False):
  2.   grid = np.array([list(line) for line in s.splitlines()])
  3.   current = set(tuple(yx) for yx in np.argwhere(grid == '#'))
  4.   offsets8 = set(itertools.product((-1, 0, 1), repeat=2)) - {(0, 0)}
  5.   dirs = [(-1, 0), (1, 0), (0, -1), (0, 1)]
  6.  
  7.   for round_index in range(10**8 if part2 else 10):
  8.     proposed = {}
  9.     for y, x in current:
  10.       if any((y + dy, x + dx) in current for dy, dx in offsets8):
  11.         for dy, dx in dirs:
  12.           neighb3 = [tuple((i if c == 0 else c) for c in (dy, dx)) for i in (-1, 0, 1)]
  13.           if not any((y + dy2, x + dx2) in current for dy2, dx2 in neighb3):
  14.             proposed[y, x] = y + dy, x + dx
  15.             break
  16.  
  17.     dirs = dirs[1:] + dirs[0:1]
  18.     counter = collections.Counter(proposed.values())
  19.     if part2 and 1 not in counter.values():
  20.       return round_index + 1  # Motivation for increment by 1 is unclear.
  21.  
  22.     for yx in current.copy():
  23.       if yx in proposed and counter[proposed[yx]] == 1:
  24.         current.remove(yx)
  25.         current.add(proposed[yx])
  26.  
  27.   return np.prod(np.ptp(list(current), 0) + 1) - len(current)
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement