-
-
Save angch/831801a7660da19fc69e8bcd0bb369f6 to your computer and use it in GitHub Desktop.
Cellular Automata Sierpinski's Triangle https://go.dev/play/p/JZ5JVjIfMcD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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