12
\$\begingroup\$

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!

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

11 Answers 11

8
\$\begingroup\$

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])
\$\endgroup\$
0
5
\$\begingroup\$

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!

\$\endgroup\$
5
\$\begingroup\$

Wolfram Language (Mathematica), 31 bytes

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

Try it online!

CellularAutomaton assumes the input list is cyclic.

\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
0
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

Jelly, 7 bytes

ṙØ+^/$¡

Try it online!

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

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Ruby, 59 bytes

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

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.