Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def day23(s, *, part2=False):
- grid = np.array([list(line) for line in s.splitlines()])
- current = set(tuple(yx) for yx in np.argwhere(grid == '#'))
- offsets8 = set(itertools.product((-1, 0, 1), repeat=2)) - {(0, 0)}
- dirs = [(-1, 0), (1, 0), (0, -1), (0, 1)]
- for round_index in range(10**8 if part2 else 10):
- proposed = {}
- for y, x in current:
- if any((y + dy, x + dx) in current for dy, dx in offsets8):
- for dy, dx in dirs:
- neighb3 = [tuple((i if c == 0 else c) for c in (dy, dx)) for i in (-1, 0, 1)]
- if not any((y + dy2, x + dx2) in current for dy2, dx2 in neighb3):
- proposed[y, x] = y + dy, x + dx
- break
- dirs = dirs[1:] + dirs[0:1]
- counter = collections.Counter(proposed.values())
- if part2 and 1 not in counter.values():
- return round_index + 1 # Motivation for increment by 1 is unclear.
- for yx in current.copy():
- if yx in proposed and counter[proposed[yx]] == 1:
- current.remove(yx)
- current.add(proposed[yx])
- return np.prod(np.ptp(list(current), 0) + 1) - len(current)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement