Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def apply_cycles(s, num_cycles=6, dim=3):
- lines = s.strip('\n').split('\n')
- indices = {
- (0,) * (dim - 2) + (y, x)
- for y, line in enumerate(lines) for x, ch in enumerate(line)
- if ch == '#'
- }
- offsets = set(itertools.product(*((-1, 0, 1),) * dim)) - {(0,) * dim}
- def neighbors(index):
- for offset in offsets:
- yield tuple(map(sum, zip(index, offset)))
- def count_neighbors(index):
- return sum(neighbor in indices for neighbor in neighbors(index))
- for _ in range(num_cycles):
- survivors = {index for index in indices if 2 <= count_neighbors(index) <= 3}
- adjacents = {neighbor for index in indices for neighbor in neighbors(index)}
- births = {index for index in adjacents if count_neighbors(index) == 3}
- indices = survivors | births
- return len(indices)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement