Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
lru_cache solution in Speedy category for Life Counter by gyahun_dash
from collections import Counter
from functools import lru_cache
from itertools import chain, product
capacities = {True: (2, 3), False: (3,)}
@lru_cache(maxsize=256)
def neighbors(c):
return set(product(*[(z - 1, z, z + 1) for z in c])) - {c}
def life_counter(state, ticks):
cells = product(range(len(state)), range(len(state[0])))
lives = {(y, x) for y, x in cells if state[y][x]}
for t in range(ticks):
freqs = Counter(chain.from_iterable(map(neighbors, lives)))
lives = {c for c, f in freqs.items() if f in capacities[c in lives]}
return len(lives)
Oct. 1, 2014
Comments: