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

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!

jaaq

Posted 2019-09-17T10:42:47.907

Reputation: 499

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

Answers

8

05AB1E, 14 13 10 9 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])

Grimmy

Posted 2019-09-17T10:42:47.907

Reputation: 12 521

5

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!

TFeld

Posted 2019-09-17T10:42:47.907

Reputation: 19 246

5

Wolfram Language (Mathematica), 31 bytes

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

Try it online!

CellularAutomaton assumes the input list is cyclic.

attinat

Posted 2019-09-17T10:42:47.907

Reputation: 3 495

2

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!

Arnauld

Posted 2019-09-17T10:42:47.907

Reputation: 111 334

2

Japt -mh, 11 10 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

Shaggy

Posted 2019-09-17T10:42:47.907

Reputation: 24 623

2

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.

Neil

Posted 2019-09-17T10:42:47.907

Reputation: 95 035

2

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

Adám

Posted 2019-09-17T10:42:47.907

Reputation: 37 779

2

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!

Chas Brown

Posted 2019-09-17T10:42:47.907

Reputation: 8 959

2

Jelly, 7 bytes

ṙØ+^/$¡

Try it online!

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

Erik the Outgolfer

Posted 2019-09-17T10:42:47.907

Reputation: 38 134

1

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

ar4093

Posted 2019-09-17T10:42:47.907

Reputation: 531

0

Ruby, 59 bytes

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

Try it online!

Reinstate Monica -- notmaynard

Posted 2019-09-17T10:42:47.907

Reputation: 1 053