Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@angch
Created November 17, 2022 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angch/831801a7660da19fc69e8bcd0bb369f6 to your computer and use it in GitHub Desktop.
Save angch/831801a7660da19fc69e8bcd0bb369f6 to your computer and use it in GitHub Desktop.
Cellular Automata Sierpinski's Triangle https://go.dev/play/p/JZ5JVjIfMcD
package main
import (
"fmt"
"strings"
)
func cell(cells []byte, rule int) {
t := [8]byte{}
for i := 0; i < 8; i++ {
switch rule & 1 {
case 0:
t[i] = ' '
case 1:
t[i] = 'X'
}
rule >>= 1
}
for row := 0; row < 100; row++ {
fmt.Println(string(cells))
state := 0
if cells[0] == 'X' {
state = 1
}
for i := 1; i < len(cells); i++ {
state = (state << 1) & 7
if cells[i] == 'X' {
state = state | 1
}
cells[i-1] = t[state]
}
state = (state << 1) & 7
cells[len(cells)-1] = t[state]
}
}
func main() {
buf := []byte(strings.Repeat(" ", 120))
// single X in the middle
buf[len(buf)/2] = 'X'
cell(buf, 18) // 18 = 00010010 binary
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment