Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Sandbox

Post History

66%
+2 −0
Sandbox Implement Rule 110 [FINALIZED]

posted 3y ago by celtschk‭  ·  edited 1y ago by trichoplax‭

#4: Post edited by user avatar trichoplax‭ · 2023-06-19T10:24:56Z (over 1 year ago)
Add finalized tag now that the sandbox can be filtered to exclude tags
Implement Rule 110 [FINALIZED]
[Rule 110] is a Turing complete cellular automaton. It is defined as follows:

Take as initial value a sequence of symbols that's infinite to both sides, which consists only of two different symbols (I'm going to use $+$ and $-$ here). This sequence can be seen as a function that maps the integers to the set $\\{+,-\\}$.

Then in each iteration, calculate a new such sequence as follows:

To determine the new value at position $n$, look at the old values in the cells $n-1$, $n$ and $n+1$, and then look up the new value in the following table:

<div>
\begin{align}
  \begin{array}{c|cccccccc}
  \text{old} & +++ & ++- & +-+ & +-- & -++ & -+- & --+ & ---\\
  \hline
  \text{new} &  -  &  +  &  +  &  -  &  +  &  +  &  +  &  -
  \end{array}
\end{align}
</div>

Your task is to implement Rule 110. In particular, your program shall take as input:

  * A non-empty string that represents a pattern which is periodically repeated to both sides, starting at position 0 (so if the string is `+-`, then there's a `+` on all even positions, and a `-` on all odd positions.

  * A second (empty or non-empty) string that represents a local deviation from that pattern, again starting at position 0. for example, with the above background pattern, if the second string is `---+++`, then the initial state of the cellular automaton is
```
...+-+-+-+----++++-+-+-...
           ↑
       position 0
```

  * A number of iterations, which may be zero or positive.

It then outputs the relevant part resulting state of the state space as string. More exactly, if your pattern has length $p$ your local disturbance has length $l$, and the number of iterations is $n$, then you need to print the output space from position $-2p-n$ up to position $l+2p+n-1$ inclusive.

You may use arbitrary sequence types (such as list or tuples) instead of strings. Also you may use arbitrary characters instead of `+` and `-`, or values of other types (e.g. integers or logical values), as long as you have exactly two of them and state clearly which of them corresponds to $+$ and which to $-$.

This is <span class="badge is-tag">code-golf</span>, thus the shortest code wins.

Test cases:

```
Pattern: "-"
Local: ""
Iterations: 0
----
Output: "----"

Pattern: "-"
Local: "+"
Iterations: 0
----
Output: "--+--"

Pattern: "-"
Local: "-"
Iterations: 0
----
Output: "-----"

Pattern: "+-"
Local: "-"
Iterations: 0
----
Output: "+-+---+-+"

Pattern: "-+"
Local: "-"
Iterations: 0
----
Output: "-+-+-+-+-"

Pattern: "+"
Local: ""
Iterations: 1
----
Output: "------"

Pattern: "+-"
Local: ""
Iterations: 1
----
Output: "++++++++++"

Pattern: "-"
Local: "+"
Iterations: 1
----
Output: "--++---"

Pattern: "-"
Local: "+"
Iterations: 3
----
Output: "--++-+-----"

Pattern: "+---"
Local: ""
Iterations: 3
----
Output: "++-+++-+++-+++-+++-+++"

Pattern: "+-"
Local: "-"
Iterations: 3
----
Output: "----+++++------"
```

**Note:** Please verify the test cases; I've evaluated them by hand, so I might have made a mistake.


[Rule 110]: https://en.wikipedia.org/wiki/Rule_110
#3: Post edited by user avatar celtschk‭ · 2021-11-16T09:18:46Z (about 3 years ago)
finalized
  • Implement Rule 110
  • Implement Rule 110 [FINALIZED]
#2: Post edited by user avatar celtschk‭ · 2021-10-08T19:28:41Z (about 3 years ago)
Fixed test case thanks to @Hakerh400
  • [Rule 110] is a Turing complete cellular automaton. It is defined as follows:
  • Take as initial value a sequence of symbols that's infinite to both sides, which consists only of two different symbols (I'm going to use $+$ and $-$ here). This sequence can be seen as a function that maps the integers to the set $\\{+,-\\}$.
  • Then in each iteration, calculate a new such sequence as follows:
  • To determine the new value at position $n$, look at the old values in the cells $n-1$, $n$ and $n+1$, and then look up the new value in the following table:
  • <div>
  • \begin{align}
  • \begin{array}{c|cccccccc}
  • \text{old} & +++ & ++- & +-+ & +-- & -++ & -+- & --+ & ---\\
  • \hline
  • \text{new} & - & + & + & - & + & + & + & -
  • \end{array}
  • \end{align}
  • </div>
  • Your task is to implement Rule 110. In particular, your program shall take as input:
  • * A non-empty string that represents a pattern which is periodically repeated to both sides, starting at position 0 (so if the string is `+-`, then there's a `+` on all even positions, and a `-` on all odd positions.
  • * A second (empty or non-empty) string that represents a local deviation from that pattern, again starting at position 0. for example, with the above background pattern, if the second string is `---+++`, then the initial state of the cellular automaton is
  • ```
  • ...+-+-+-+----++++-+-+-...
  • position 0
  • ```
  • * A number of iterations, which may be zero or positive.
  • It then outputs the relevant part resulting state of the state space as string. More exactly, if your pattern has length $p$ your local disturbance has length $l$, and the number of iterations is $n$, then you need to print the output space from position $-2p-n$ up to position $l+2p+n-1$ inclusive.
  • You may use arbitrary sequence types (such as list or tuples) instead of strings. Also you may use arbitrary characters instead of `+` and `-`, or values of other types (e.g. integers or logical values), as long as you have exactly two of them and state clearly which of them corresponds to $+$ and which to $-$.
  • This is <span class="badge is-tag">code-golf</span>, thus the shortest code wins.
  • Test cases:
  • ```
  • Pattern: "-"
  • Local: ""
  • Iterations: 0
  • ----
  • Output: "----"
  • Pattern: "-"
  • Local: "+"
  • Iterations: 0
  • ----
  • Output: "--+--"
  • Pattern: "-"
  • Local: "-"
  • Iterations: 0
  • ----
  • Output: "-----"
  • Pattern: "+-"
  • Local: "-"
  • Iterations: 0
  • ----
  • Output: "+-+---+-+"
  • Pattern: "-+"
  • Local: "-"
  • Iterations: 0
  • ----
  • Output: "-+-+-+-+-"
  • Pattern: "+"
  • Local: ""
  • Iterations: 1
  • ----
  • Output: "------"
  • Pattern: "+-"
  • Local: ""
  • Iterations: 1
  • ----
  • Output: "++++++"
  • Pattern: "-"
  • Local: "+"
  • Iterations: 1
  • ----
  • Output: "--++---"
  • Pattern: "-"
  • Local: "+"
  • Iterations: 3
  • ----
  • Output: "--++-+-----"
  • Pattern: "+---"
  • Local: ""
  • Iterations: 3
  • ----
  • Output: "++-+++-+++-+++-+++-+++"
  • Pattern: "+-"
  • Local: "-"
  • Iterations: 3
  • ----
  • Output: "----+++++------"
  • ```
  • **Note:** Please verify the test cases; I've evaluated them by hand, so I might have made a mistake.
  • [Rule 110]: https://en.wikipedia.org/wiki/Rule_110
  • [Rule 110] is a Turing complete cellular automaton. It is defined as follows:
  • Take as initial value a sequence of symbols that's infinite to both sides, which consists only of two different symbols (I'm going to use $+$ and $-$ here). This sequence can be seen as a function that maps the integers to the set $\\{+,-\\}$.
  • Then in each iteration, calculate a new such sequence as follows:
  • To determine the new value at position $n$, look at the old values in the cells $n-1$, $n$ and $n+1$, and then look up the new value in the following table:
  • <div>
  • \begin{align}
  • \begin{array}{c|cccccccc}
  • \text{old} & +++ & ++- & +-+ & +-- & -++ & -+- & --+ & ---\\
  • \hline
  • \text{new} & - & + & + & - & + & + & + & -
  • \end{array}
  • \end{align}
  • </div>
  • Your task is to implement Rule 110. In particular, your program shall take as input:
  • * A non-empty string that represents a pattern which is periodically repeated to both sides, starting at position 0 (so if the string is `+-`, then there's a `+` on all even positions, and a `-` on all odd positions.
  • * A second (empty or non-empty) string that represents a local deviation from that pattern, again starting at position 0. for example, with the above background pattern, if the second string is `---+++`, then the initial state of the cellular automaton is
  • ```
  • ...+-+-+-+----++++-+-+-...
  • position 0
  • ```
  • * A number of iterations, which may be zero or positive.
  • It then outputs the relevant part resulting state of the state space as string. More exactly, if your pattern has length $p$ your local disturbance has length $l$, and the number of iterations is $n$, then you need to print the output space from position $-2p-n$ up to position $l+2p+n-1$ inclusive.
  • You may use arbitrary sequence types (such as list or tuples) instead of strings. Also you may use arbitrary characters instead of `+` and `-`, or values of other types (e.g. integers or logical values), as long as you have exactly two of them and state clearly which of them corresponds to $+$ and which to $-$.
  • This is <span class="badge is-tag">code-golf</span>, thus the shortest code wins.
  • Test cases:
  • ```
  • Pattern: "-"
  • Local: ""
  • Iterations: 0
  • ----
  • Output: "----"
  • Pattern: "-"
  • Local: "+"
  • Iterations: 0
  • ----
  • Output: "--+--"
  • Pattern: "-"
  • Local: "-"
  • Iterations: 0
  • ----
  • Output: "-----"
  • Pattern: "+-"
  • Local: "-"
  • Iterations: 0
  • ----
  • Output: "+-+---+-+"
  • Pattern: "-+"
  • Local: "-"
  • Iterations: 0
  • ----
  • Output: "-+-+-+-+-"
  • Pattern: "+"
  • Local: ""
  • Iterations: 1
  • ----
  • Output: "------"
  • Pattern: "+-"
  • Local: ""
  • Iterations: 1
  • ----
  • Output: "++++++++++"
  • Pattern: "-"
  • Local: "+"
  • Iterations: 1
  • ----
  • Output: "--++---"
  • Pattern: "-"
  • Local: "+"
  • Iterations: 3
  • ----
  • Output: "--++-+-----"
  • Pattern: "+---"
  • Local: ""
  • Iterations: 3
  • ----
  • Output: "++-+++-+++-+++-+++-+++"
  • Pattern: "+-"
  • Local: "-"
  • Iterations: 3
  • ----
  • Output: "----+++++------"
  • ```
  • **Note:** Please verify the test cases; I've evaluated them by hand, so I might have made a mistake.
  • [Rule 110]: https://en.wikipedia.org/wiki/Rule_110
#1: Initial revision by user avatar celtschk‭ · 2021-10-06T09:32:54Z (about 3 years ago)
Implement Rule 110
[Rule 110] is a Turing complete cellular automaton. It is defined as follows:

Take as initial value a sequence of symbols that's infinite to both sides, which consists only of two different symbols (I'm going to use $+$ and $-$ here). This sequence can be seen as a function that maps the integers to the set $\\{+,-\\}$.

Then in each iteration, calculate a new such sequence as follows:

To determine the new value at position $n$, look at the old values in the cells $n-1$, $n$ and $n+1$, and then look up the new value in the following table:

<div>
\begin{align}
  \begin{array}{c|cccccccc}
  \text{old} & +++ & ++- & +-+ & +-- & -++ & -+- & --+ & ---\\
  \hline
  \text{new} &  -  &  +  &  +  &  -  &  +  &  +  &  +  &  -
  \end{array}
\end{align}
</div>

Your task is to implement Rule 110. In particular, your program shall take as input:

  * A non-empty string that represents a pattern which is periodically repeated to both sides, starting at position 0 (so if the string is `+-`, then there's a `+` on all even positions, and a `-` on all odd positions.

  * A second (empty or non-empty) string that represents a local deviation from that pattern, again starting at position 0. for example, with the above background pattern, if the second string is `---+++`, then the initial state of the cellular automaton is
```
...+-+-+-+----++++-+-+-...
           ↑
       position 0
```

  * A number of iterations, which may be zero or positive.

It then outputs the relevant part resulting state of the state space as string. More exactly, if your pattern has length $p$ your local disturbance has length $l$, and the number of iterations is $n$, then you need to print the output space from position $-2p-n$ up to position $l+2p+n-1$ inclusive.

You may use arbitrary sequence types (such as list or tuples) instead of strings. Also you may use arbitrary characters instead of `+` and `-`, or values of other types (e.g. integers or logical values), as long as you have exactly two of them and state clearly which of them corresponds to $+$ and which to $-$.

This is <span class="badge is-tag">code-golf</span>, thus the shortest code wins.

Test cases:

```
Pattern: "-"
Local: ""
Iterations: 0
----
Output: "----"

Pattern: "-"
Local: "+"
Iterations: 0
----
Output: "--+--"

Pattern: "-"
Local: "-"
Iterations: 0
----
Output: "-----"

Pattern: "+-"
Local: "-"
Iterations: 0
----
Output: "+-+---+-+"

Pattern: "-+"
Local: "-"
Iterations: 0
----
Output: "-+-+-+-+-"

Pattern: "+"
Local: ""
Iterations: 1
----
Output: "------"

Pattern: "+-"
Local: ""
Iterations: 1
----
Output: "++++++"

Pattern: "-"
Local: "+"
Iterations: 1
----
Output: "--++---"

Pattern: "-"
Local: "+"
Iterations: 3
----
Output: "--++-+-----"

Pattern: "+---"
Local: ""
Iterations: 3
----
Output: "++-+++-+++-+++-+++-+++"

Pattern: "+-"
Local: "-"
Iterations: 3
----
Output: "----+++++------"
```

**Note:** Please verify the test cases; I've evaluated them by hand, so I might have made a mistake.


[Rule 110]: https://en.wikipedia.org/wiki/Rule_110