12
2
This question just trended over on code-review and I figured you might like it adapted as a codegolf challenge:
You are given a non-empty list of x houses represented as booleans. 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.
def get_state_as_pos(thelist, pos):
if thelist[pos-1] == thelist[pos+1]:
return 0
else:
return 1
For example, if we had a group of neighbors [0, 1, 0] then the house at [1] would become 0 since both the house to its left and right are both inactive. The cells at both ends check the opposite side as well, so the neighbors at index 0 are at index length-1
and indexn1 and vice versa.
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 steps and should output the state of the houses after the given number of steps.
input: states = [1, 0, 0, 0, 0, 1, 0, 0], steps = 1
output should be [0, 1, 0, 0, 1, 0, 1, 1]
input: states = [1, 1, 1, 0, 1, 1, 1, 1], steps = 2
intermediate state= [0, 0, 1, 0, 1, 0, 0, 0]
output should be [0, 1, 0, 0, 0, 1, 0, 0]
input: states = [1], steps=1
output: states= [0]
Take the list and steps however you like and output the resulting list via default I/O. Standard loopholes are forbidden. This is codegolf, shortest answer in bytes wins!
8+1 for Cellular Automata. Isn't this Rule 90, though? – HighlyRadioactive – 2019-09-17T10:50:39.853
2Shouldn't the first test case result in
[0, 1, 0, 0, 1, 0, 1, 1]
? – TFeld – 2019-09-17T11:15:01.933@TwilightSparkle What's rule 90? I am rather new, sorry. – jaaq – 2019-09-17T11:25:14.540
@TFeld you are indeed correct. Fixed it! – jaaq – 2019-09-17T11:25:33.243
4@jaaq I'm referring to the Elementary Cellular Automata Rule (of transformation between each step, or generations) #90. Type "Rule 90" in Wolfram|Alpha. – HighlyRadioactive – 2019-09-17T11:26:59.543
@TwilightSparkle yes it appears to be. I never got too far into Cellular Automata, this appears to be coincidence. – jaaq – 2019-09-17T11:30:25.980
12output the resulting list via STDOUT : it is highly recommended to just rely on our default I/O methods. – Arnauld – 2019-09-17T12:17:07.687
5@jaaq Not so much coincidence as there's a Rule # for every standard 1D Cellular Automata. This is because 3 bits has 8 possible states (left neighbor, self, right neighbor) and if you say for each of those states a given house will be on or off that's 8 true/false values which happens to map perfectly to a byte. Thus Rule #0-255 can be used as shorthand to describe any of these rulesets by taking the binary expression to be the resulting house on/off state in each of the 8 situations based on position in the byte. Some rules are considered to be notable such as 90, thus the recognition :) – Lunin – 2019-09-18T01:00:33.717
@Arnauld didn't know about that. Added! – jaaq – 2019-09-18T07:23:32.253
Can the number of steps be 0? – Reinstate Monica -- notmaynard – 2019-09-18T21:12:08.750
good question. What would be considered standard? – jaaq – 2019-09-23T21:43:43.430