I’m working on a practice algorithm problem, stated as follows:
There are eight houses represented as cells. Each day, the houses compete with adjacent ones. 1 represents an “active” house and 0 represents an “inactive” house. If the neighbors on both sides of a given house are either both active or both inactive, then that house becomes inactive on the next day. Otherwise it becomes active. For example, if we had a group of neighbors [0, 1, 0] then the house at  would become 0 since both the house to its left and right are both inactive. The cells at both ends only have one adjacent cell so assume that the unoccupied space on the other side is an inactive cell.
Even after updating the cell, you have to consider its prior state when updating the others so that the state information of each cell is updated simultaneously.
The function takes the array of states and a number of days and should output the state of the houses after the given number of days.
Examples: input: states = [1, 0, 0, 0, 0, 1, 0, 0], days = 1 output should be [0, 1, 0, 0, 1, 0, 1, 0] input: states = [1, 1, 1, 0, 1, 1, 1, 1], days = 2 output should be [0, 0, 0, 0, 0, 1, 1, 0]
def cell_compete(states, days): for _ in range(days): prev_cell, next_cell, index = 0, 0, 0 while index < len(states): if index < len(states) - 1: next_cell = states[index + 1] elif index == len(states) - 1: next_cell = 0 if next_cell == prev_cell: prev_cell = states[index] states[index] = 0 else: prev_cell = states[index] states[index] = 1 index += 1 return states
I originally thought to take advantage of the fact they are 0s and 1s only and to use bitwise operators, but couldn’t quite get that to work.
Can I improve the efficiency of this algorithm? Any ideas on the “optimal” solution?
Appreciate any feedback, thanks!