• Scientific- life counter,hint.bryukh

 

Hi,what's wrong with this code? I can't get response from server, is code wrong or too slow or what..thnx for answer.

def life_counter(state, tick_n):
    c = ((1,0),(1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1))
    state_a = map(list,state)
    for i in range(tick_n):
        if 1 in state_a[0]:
            state_a.insert(0, [0] * len(state_a[0]))
        if 1 in state_a[-1]:
            state_a.append([0] * len(state_a[0]))
        for i in range(len(state_a)):
            if state_a[i][0] != 0:
                map(lambda x: x.insert(0,0), state_a)
                break
        for i in range(len(state_a)):
            if state_a[i][-1] != 0:
                map(lambda x: x.append(0), state_a)
                break

        hd = len(state_a[0])
        vd = len(state_a)
        new_state = [[0] * len(state_a[0]) for n in range(len(state_a))]
        for y in range(hd):
            for x in range(vd):
                neighbours = sum([1 for x1, y1 in c if x + x1 in range(vd) and
                                  y + y1 in range(hd) and state_a[x + x1][y + y1] == 1])

                if state_a[x][y] == 1:
                    if neighbours < 2:
                        new_state[x][y] = 0
                    elif neighbours == 2 or neighbours == 3:
                        new_state[x][y] = 1
                    else:
                        new_state[x][y] = 0

                else:
                    if neighbours == 3:
                        new_state[x][y] = 1
        state_a = new_state

    return sum([sum(r) for r in new_state])
.