Canon

From Esolang
Jump to navigation Jump to search
"Rule 108" redirects here. For the elementary cellular automaton, see Rule 108.

Canon is a one-dimensional cellular automaton created by User:TheCanon2.

Rules

Canon has a single pointer. The pointed moves based on the value of the pointer cell b and its neighbours a and c. Canon has four rules.

  • If a = c != b, flip b and move the pointer two cells to the left.
  • If a = b = c, flip b and move the pointer one cell to the left.
  • If a != c and b = 1, flip b and move the pointer two cells to the right.
  • If a != c and b = 0, flip b and move the pointer one cell to the right.

Implementations

The following Amtu script defines Canon as a Turing machine.

A < R < R < R
R > Z > Z > O
Z > ZZ > ZZ > ZO
O > OZ > OZ > OO
ZZ <1< A <1< A <1>> A
ZO <0<< A <0<< A <0> A
OZ <1>> A <1<< A <1<< A
OO <0> A <0> A <0< A

The following Python script performs Canon.

tape = [0] # Canon
import random
for i in range(0, 60):
    tape.append(random.randint(0, 1)) # Replace with 0 for blank input
pointer = 30
while True:
    subtape = [tape[pointer-1],tape[pointer],tape[pointer+1]]
    if subtape == [0, 0, 0]:
        tape[pointer] = (tape[pointer] + 1) % 2
        pointer -= 1
    elif subtape == [0, 0, 1]:
        tape[pointer] = (tape[pointer] + 1) % 2
        pointer += 2
    elif subtape == [0, 1, 0]:
        tape[pointer] = (tape[pointer] + 1) % 2
        pointer -= 2
    elif subtape == [0, 1, 1]:
        tape[pointer] = (tape[pointer] + 1) % 2
        pointer += 1
    elif subtape == [1, 0, 0]:
        tape[pointer] = (tape[pointer] + 1) % 2
        pointer += 2
    elif subtape == [1, 0, 1]:
        tape[pointer] = (tape[pointer] + 1) % 2
        pointer -= 2
    elif subtape == [1, 1, 0]:
        tape[pointer] = (tape[pointer] + 1) % 2
        pointer += 1
    elif subtape == [1, 1, 1]:
        tape[pointer] = (tape[pointer] + 1) % 2
        pointer -= 1
    for i in range(0, len(tape)):
        print(tape[i], end="")
    print("\n", end="")