Guest User

Untitled

a guest
Oct 11th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.88 KB | None | 0 0
  1. import (
  2.     "math/rand"
  3.     "bytes"
  4.     "time"
  5.     "fmt"
  6.     "os"
  7. )
  8.  
  9. type CellularAutomata struct {
  10.     rule     []byte
  11.     state    []byte
  12.     left     byte
  13.     mid      byte
  14.     right    byte
  15.     rule_idx byte
  16. }
  17.  
  18. func cal_index(bl, bm, br byte) byte { return 4*bl + 2*bm + br }
  19.  
  20. func (c *CellularAutomata) Step(grow bool) {
  21.  
  22.     new_state := make([]byte, len(c.state)+2)
  23.     offset := 0
  24.  
  25.     for curr := range c.state {
  26.         if curr == 0 {
  27.             if grow {
  28.                 c.left = c.state[len(c.state)-1]
  29.             } else {
  30.                 c.left = 0
  31.             }
  32.             c.mid = c.state[0]
  33.             c.right = c.state[1]
  34.         } else if curr == len(c.state)-1 {
  35.             c.left = c.state[curr-1]
  36.             c.mid = c.state[curr]
  37.             if grow {
  38.                 c.right = c.state[0]
  39.             } else {
  40.                 c.right = 0
  41.             }
  42.         } else {
  43.             c.left = c.state[curr-1]
  44.             c.mid = c.state[curr]
  45.             c.right = c.state[curr+1]
  46.         }
  47.         c.rule_idx = cal_index(c.left, c.mid, c.right)
  48.         if grow {
  49.             offset = 1
  50.         }
  51.         new_state[curr+offset] = c.rule[c.rule_idx]
  52.     }
  53.  
  54.     c.state = new_state
  55. }
  56.  
  57. func do(ca CellularAutomata, rounds int) time.Duration {
  58.  
  59.     start := time.Now()
  60.     //fmt.Fprintf(os.Stdout, "%v: begin\n", time.Now())
  61.  
  62.     for i := 0; i < rounds; i++ {
  63.         //fmt.Fprintf(os.Stdout, "%3d %v\n", i, ca.state)
  64.         ca.Step(false)
  65.     }
  66.  
  67.     elapsed := time.Since(start)
  68.     fmt.Fprintf(os.Stdout, "..%v rounds..took: %v\n", rounds, elapsed)
  69.  
  70.     return elapsed
  71. }
  72.  
  73. func find(ca CellularAutomata, target []byte) (int, time.Duration) {
  74.     start := time.Now()
  75.     round := 0
  76.     for {
  77.         if bytes.Equal(ca.state, target) {
  78.             elapsed := time.Since(start)
  79.             fmt.Fprintf(os.Stdout, "..%v rounds..took: %v\n", round, elapsed)
  80.             return round, elapsed
  81.         }
  82.         if round % 10000000 == 0 {
  83.             elapsed := time.Since(start)
  84.             fmt.Fprintf(os.Stdout, "..%v rounds..took: %v\n", round, elapsed)
  85.             fmt.Fprintf(os.Stdout, "current state: %v\n", ca.state)
  86.         }
  87.         ca.Step(false)
  88.         round++
  89.     }
  90. }
  91.  
  92. func rand_byte_array(len int) []byte {
  93.     rand.Seed(time.Now().Unix())
  94.     b := make([]byte, len)
  95.     for x := 0; x<len; x+=1 {
  96.         b[x] = byte(rand.Intn(2))
  97.     }
  98.     return b
  99. }
  100.  
  101. func main() {
  102.  
  103.     ca := CellularAutomata{}
  104.  
  105.     // Wolframs Rule 30
  106.     p := []byte{0, 1, 1, 1, 1, 0, 0, 0}
  107.     ca.rule = p
  108.  
  109.     // Starting state
  110.     q := []byte{1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0}
  111.     ca.state = q
  112.  
  113.     fmt.Fprintf(os.Stdout, "target: %v\n", rand_byte_array(1000))
  114.  
  115.     for x := 0; x<30; x+=1 {
  116.         fmt.Fprintf(os.Stdout, "current state: %v\n", ca.state)
  117.         ca.Step(true)
  118.     }
  119.  
  120.     /*
  121.     m := make(map[byte]time.Duration)
  122.     for x := 0; x<100000; x+=10000 { m[x] = do(ca, x) }
  123.     */
  124.  
  125.     //x, y := find(ca, q)
  126.     //fmt.Fprintf(os.Stdout, "%v %v\n", x, y)
  127.  
  128.     //fmt.Fprintf(os.Stdout, "%v\n", m)
  129. }
Advertisement
Add Comment
Please, Sign In to add comment