# Simulate a 1D Game-of-Life-ish Model

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!

• +1 for Cellular Automata. Isn't this Rule 90, though? – null Sep 17 '19 at 10:50
• Shouldn't the first test case result in [0, 1, 0, 0, 1, 0, 1, 1]? – TFeld Sep 17 '19 at 11:15
• @jaaq I'm referring to the Elementary Cellular Automata Rule (of transformation between each step, or generations) #90. Type "Rule 90" in Wolfram|Alpha. – null Sep 17 '19 at 11:26
• output the resulting list via STDOUT : it is highly recommended to just rely on our default I/O methods. – Arnauld Sep 17 '19 at 12:17
• @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 Sep 18 '19 at 1:00

# 05AB1E, 1413109 6 bytes

Based on Shaggy's Japt solution

F©Á®À^


Try it online!

F                  # repeat n times:
©Á                #  the list, rotated right
®À              #  the list, rotated left
^             #  xor (vectorizes)


Unnecessarily clever 9-byte solution:

F¥DO.øü+É


Try it online!

F                  # repeat n times:
#  (examples given for the initial state [0, 1, 1, 0, 1])
¥                 #  deltas of the list ([1, 0, -1, 1])
D                #  duplicate
O               #  sum (1)
.ø             #  surround ([1, 1, 0, -1, 1, 1])
ü+           #  pairwise addition ([2, 1, -1, 0, 2])
É          #  modulo 2 ([0, 1, 1, 0, 0])


# Python 2, 72 bytes

f=lambda s,n:n and f([a^b for a,b in zip(s[-1:]+s,s[1:]+s[:1])],n-1)or s


Try it online!

# Wolfram Language (Mathematica), 31 bytes

CellularAutomaton[90,#,{{#2}}]&


Try it online!

CellularAutomaton assumes the input list is cyclic.

# JavaScript (ES6), 57 bytes

Takes input as (steps)(array).

s=>g=a=>s--?g(a.map(_=>a[~-i++%l]^a[i%l],i=l=a.length)):a


Try it online!

# Japt-mh, 1110 9 bytes

I/O of states as singleton 2D arrays.

VÇí^Zé2)é


Try it

VÇí^Zé2)é     :Implicit input of integer U=steps & array V=[states]
VÇ            :Modify the last element Z in V
í           :Interleave with
Zé2       :  Z rotated right twice and
^          :  Reduce each pair by XOR
)      :End interleave
é     :Rotate right once
:Repeat U times and implicitly output V


# Retina, 51 bytes

1A
"$+"{(.).*(.)$2$&$1
(.)(?=.(\1|(.)))?
$#2*$#3


Try it online! Takes the number of steps on the first line and a string of 0s and 1s on the second line. Explanation:

1A


Delete the number of steps from the input.

"$+"{  Repeat that number times. (.).*(.)$2$&$1


Copy the end digits to the other ends to simulate wrapping.

(.)(?=.(\1|(.)))?
$#2*$#3


Perform the XOR operation.

# APL (Dyalog Extended), 12 bytesSBCS

Full program. Prompts stdin for array of states and then for number of steps. Prints to stdout.

1(⌽≠⌽⍢⌽)⍣⎕⊢⎕


Try it online!

⎕ get evaluated input from console (array of states)

⊢ on that, apply…

1()⍣⎕ the following tacit function, input number of times, each time with 1 as left argument:

⌽⍢⌽ rotate the right argument 1 step left while reversed (i.e. rotate one step right)

⌽≠ XOR with the argument rotated 1 step left

# Python 2, 71 bytes

f=lambda a,n:n and f([a[i-1]^(a+a)[i+1]for i in range(len(a))],n-1)or a


Try it online!

# Jelly, 7 bytes

ṙØ+^/\$¡


Try it online!

Full program. A singleton output is represented as x instead of [x].

# Pyth, 24 bytes

AQVH=Gmxhded.:+eG+GhG3;G


Try it online!

AQ                        # G, H = Q[0], Q[1] # Q = input in the form [[states],steps]
VH                      # for i in range(H):
=G                    # G =
m                   #     map(lambda d:                              )
xhded              #                   d[0] ^ d[-1],
.:       3    #         substrings(                 , length=3)
+eG+GhG     #                     G[-1] + G + G[0]
;   # (end for loop)
G  # print G


# Ruby, 59 bytes

->r,n{n.times{r=r.rotate.zip(r.rotate -1).map{|a,b|a^b}};r}


Try it online!