Skip to content

Instantly share code, notes, and snippets.

@dlozeve
Last active November 24, 2020 09:21
Show Gist options
  • Save dlozeve/903c9d5dcb06f8165321154d100ef091 to your computer and use it in GitHub Desktop.
Save dlozeve/903c9d5dcb06f8165321154d100ef091 to your computer and use it in GitHub Desktop.
Rule 30 cellular automaton in Dyalog APL
⎕io←0
⍝ Stencil uses zero boundaries
r30←{(7-2⊥⍵)⌷(8⍴2)⊤30}⌺3
⍝ Other version, slightly shorter but less legible
r30←{⊃30⊤⍨2⍴⍨1+2⊥⍵}⌺3
⍝ Single alive cell initialization
init←(30⍴0),1,30⍴0
' █'[↑{(r30⍣⍵)init}¨⍳30]
⍝ Random initialization
init←?79⍴2
' █'[↑{(r30⍣⍵)init}¨⍳30]
⍝ Arbitrary 1D cellular automaton with cyclical boundaries
⍝ Left argument is the rule number
ca←{⊃¨⍺⊤⍨¨2⍴⍨¨1+2⊥↑¯1 0 1∘.⌽⊂⍵}
' █'[↑{((30∘ca)⍣⍵)(30⍴0),1,30⍴0}¨⍳30]
⍝ █
⍝ ███
⍝ ██ █
⍝ ██ ████
⍝ ██ █ █
⍝ ██ ████ ███
⍝ ██ █ █ █
⍝ ██ ████ ██████
⍝ ██ █ ███ █
⍝ ██ ████ ██ █ ███
⍝ ██ █ █ ████ ██ █
⍝ ██ ████ ██ █ █ ████
⍝ ██ █ ███ ██ ██ █ █
⍝ ██ ████ ██ ███ ███ ██ ███
⍝ ██ █ █ ███ █ ███ █ █
⍝ ██ ████ ██ █ █ █████ ███████
⍝ ██ █ ███ ████ █ ███ █
⍝ ██ ████ ██ ███ ██ ██ █ ███
⍝ ██ █ █ ███ █ ██ ███ ████ ██ █
⍝ ██ ████ ██ █ ██████ █ █ ███ ████
⍝ ██ █ ███ ████ ████ ███ ██ █ █
⍝ ██ ████ ██ ███ █ ██ █ █ █ ███ ███
⍝ ██ █ █ ███ █ ███ ██ █ ███ ██ █ █ █ █
⍝ ██ ████ ██ █ ███ █ █ ████ █ █ ██ ██████
⍝ ██ █ ███ ████ ██ █████ █ █████ █ █ █
⍝ ██ ████ ██ ███ █ ██ █ █ ██ █ █████ ███
⍝ ██ █ █ ███ █ ██ █ ████ ██ █ ██ ██ █ ██ █
⍝ ██ ████ ██ █ ███ █ █ █ ███ ████ █ ██ █ ██ █ ████
⍝ ██ █ ███ ████ ████ ██ ██ ███ █ █ ████ █ █ █
⍝ ██ ████ ██ ███ █ ██ █ █ ███ █ ██ ████ ███ ██ ███
⍝ Rule 110
' █'[↑{((110∘ca)⍣⍵)60⍴(14⍴0),1}¨⍳30]
⍝ █ █ █ █
⍝ ██ ██ ██ ██
⍝ ███ ███ ███ ███
⍝ ██ █ ██ █ ██ █ ██ █
⍝ █████ █████ █████ █████
⍝ ██ █ ██ █ ██ █ ██ █
⍝ ███ ██ ███ ██ ███ ██ ███ ██
⍝ ██ █ ███ ██ █ ███ ██ █ ███ ██ █ ███
⍝ ███████ █ ███████ █ ███████ █ ███████ █
⍝ ██ ███ ██ ███ ██ ███ ██ ███
⍝ ███ ██ █ ███ ██ █ ███ ██ █ ███ ██ █
⍝ ██ █ █████ ██ █ █████ ██ █ █████ ██ █ █████
⍝ █████ ██ █ █████ ██ █ █████ ██ █ █████ ██ █
⍝ ██ █ ███ ██ ██ █ ███ ██ ██ █ ███ ██ ██ █ ███ ██
⍝ ███ ████ █ ██████ ████ █ ██████ ████ █ ██████ ████ █ ███
⍝ █ ██ █████ █ ██ █████ █ ██ █████ █ ██ █████
⍝ █████ ██ █ █████ ██ █ █████ ██ █ █████ ██ █
⍝ ██ ████ ██ ██ ████ ██ ██ ████ ██ ██ ████ ██
⍝ ██ ██ █ ███ ███ ██ █ ███ ███ ██ █ ███ ███ ██ █ ███ █
⍝ █ ███ ████ ███ █ ███ ████ ███ █ ███ ████ ███ █ ███ ████ ███
⍝ ████ ███ ███ █████ ███ ███ █████ ███ ███ █████ ███ ███ █
⍝ ███ █ ██ ███ ███ █ ██ ███ ███ █ ██ ███ ███ █ ██ ███
⍝ ██ ████████ █ ██ ████████ █ ██ ████████ █ ██ ████████ █
⍝ █████ ███ █████ ███ █████ ███ █████ ███
⍝ ██ █ ██ ███ █ ██ ███ █ ██ ███ █ ██ █
⍝ █ ██ █████ █ ██ █████ █ ██ █████ █ ██ █████
⍝ ██ ███ ██ ███ ███ ██ ███ ███ ██ ███ ███ ██ █
⍝ ███ █ ███ ██ ███ █ ███ ██ ███ █ ███ ██ ███ █ ███ ██
⍝ ██ ███ ██ █ █████ ███ ██ █ █████ ███ ██ █ █████ ███ ██ █ ███
⍝ ███ ████████ ███ ████████ ███ ████████ ███ ████████
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment