Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
clear solution in Clear category for Life Counter by sidorov
def life_counter(start, n):
from collections import Counter
neib = lambda x, y: {(x + i, y + j) for i in (-1, 0, 1) for j in (-1, 0, 1)} - {(x, y)}
alive = {(x, y): neib(x, y) for x in range(len(start[0])) for y in range(len(start)) if start[y][x]}
for _ in range(n):
all_neib = Counter()
for val in alive.values():
all_neib += Counter(val)
will_born = {a for a, b in all_neib.items() if b == 3}.difference(alive.keys())
will_live = {a for a, b in alive.items() if len(b.intersection(alive.keys())) in (2, 3) }
alive = {a: neib(*a) for a in (will_born.union(will_live))}
return len(alive)
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert life_counter(((0, 1, 0, 0, 0, 0, 0),
(0, 0, 1, 0, 0, 0, 0),
(1, 1, 1, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 1, 1),
(0, 0, 0, 0, 0, 1, 1),
(0, 0, 0, 0, 0, 0, 0),
(1, 1, 1, 0, 0, 0, 0)), 4) == 15, "Example"
assert life_counter(((0, 1, 0, 0, 0, 0, 0),
(0, 0, 1, 0, 0, 0, 0),
(1, 1, 1, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 1, 1),
(0, 0, 0, 0, 0, 1, 1),
(0, 0, 0, 0, 0, 0, 0),
(1, 1, 1, 0, 0, 0, 0)), 15) == 14, "Little later"
assert life_counter(((0, 1, 0),
(0, 0, 1),
(1, 1, 1)), 50) == 5, "Glider"
assert life_counter(((1, 1, 0, 1, 1),
(1, 1, 0, 1, 1),
(0, 0, 0, 0, 0),
(1, 1, 0, 1, 1),
(1, 1, 0, 1, 1)), 100) == 16, "Stones"
Feb. 8, 2023