-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator.go
More file actions
258 lines (234 loc) · 5.37 KB
/
Copy pathsimulator.go
File metadata and controls
258 lines (234 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package main
import (
"encoding/gob"
"fmt"
"math"
"os"
"runtime"
"strconv"
"time"
)
var sim [][]uint64
// make sure to initialize with a raw array of 1 and 0
func initialize(initial []uint64, evolutions int, history bool) {
for {
if initial[0] == 0 {
initial = initial[1:]
continue
} else if initial[len(initial)-1] == 0 {
initial = initial[:len(initial)-1]
continue
}
break
}
sim = nil
loopLength := int(math.Ceil(float64((len(initial))+evolutions) / 64.0))
if history == true {
sim = make([][]uint64, evolutions+1)
for i := range sim {
sim[i] = make([]uint64, loopLength)
}
} else {
sim = make([][]uint64, 2)
for i := range sim {
sim[i] = make([]uint64, loopLength)
}
}
for i, j, k, p := 0, 0, 0, 0; i < 64; i++ {
for {
if k >= ((len(sim[0]))*64 - len(initial)) { // potential issue here with >=
if p < len(initial) && initial[p] == 1 {
sim[0][j] = setBit(sim[0][j], uint64(i))
}
p++
}
k++
j++
if j == len(sim[0]) {
j = 0
break
}
}
}
}
func simulate(history bool, evolutions int, conditions []uint64) time.Duration {
generationProgress = 0
if conditions == nil {
fmt.Println("Program uninitialized using default...")
fmt.Println()
initialize(r110Default(), evolutions, history)
} else {
initialize(conditions, evolutions, history)
}
start := time.Now()
if history == true {
historicallyAware(evolutions)
} else {
historicallyUnaware(evolutions)
}
return time.Since(start)
}
func historicallyAware(evolutions int) {
for i := 1; i < evolutions+1; i++ {
sim[i][0] = ((^(sim[i-1][len(sim[i-1])-1]) << 1) & sim[i-1][0]) | (sim[i-1][0] ^ sim[i-1][1])
for j := 1; j < len(sim[i])-1; j++ {
sim[i][j] = ((^(sim[i-1][j-1])) & sim[i-1][j]) | (sim[i-1][j] ^ sim[i-1][j+1])
}
sim[i][len(sim[i])-1] = ((^(sim[i-1][len(sim[i])-2])) & sim[i-1][len(sim[i])-1]) | (sim[i-1][len(sim[i])-1] ^ sim[i-1][0]>>1)
generationProgress++
}
}
func historicallyUnaware(evolutions int) {
for i, k := 1, 0; i < evolutions+1; i, k = i+1, k^1 {
sim[k^1][0] = ((^(sim[k][len(sim[k])-1]) << 1) & sim[k][0]) | (sim[k][0] ^ sim[k][1])
for j := 1; j < len(sim[0])-1; j++ {
sim[k^1][j] = ((^(sim[k][j-1])) & sim[k][j]) | (sim[k][j] ^ sim[k][j+1])
}
sim[k^1][len(sim[0])-1] = ((^(sim[k][len(sim[0])-2])) & sim[k][len(sim[0])-1]) | (sim[k][len(sim[0])-1] ^ sim[k][0]>>1)
generationProgress++
}
}
func decompress(history bool) [][]uint64 {
// TODO reformat these loops in compliance with initialization function
if history {
out := make([][]uint64, len(sim))
for i := range out {
out[i] = make([]uint64, len(sim[0])*64)
}
lpc := 0
for i := 0; i < len(sim); i++ {
for j := 0; j < 64; j++ {
for k := 0; k < len(sim[i]); k++ {
out[i][lpc] = uint64(getBit(sim[i][k], j))
lpc++
}
}
lpc = 0
}
return out
} else {
out := make([][]uint64, 1)
for i := range out {
out[i] = make([]uint64, len(sim[0])*64)
}
if countLeadingZeros(0) < countLeadingZeros(1) {
lpc := 0
for j := 0; j < 64; j++ {
for k := 0; k < len(sim[0]); k++ {
out[0][lpc] = uint64(getBit(sim[0][k], j))
lpc++
}
}
} else {
lpc := 0
for j := 0; j < 64; j++ {
for k := 0; k < len(sim[1]); k++ {
out[1][lpc] = uint64(getBit(sim[1][k], j))
lpc++
}
}
}
return out
}
}
func countLeadingZeros(layer int) int {
zeros := 0
for j := 0; j < 64; j++ {
for k := 0; k < len(sim[layer]); k++ {
if getBit(sim[layer][k], j) == 1 {
return zeros
}
zeros++
}
}
return -1
}
func readTape() {
}
func writeToFile(path string, obj interface{}) {
file, err := os.Create(path)
if err != nil {
fmt.Println(err)
return
}
enc := gob.NewEncoder(file)
if err = enc.Encode(&obj); err != nil {
fmt.Println(err)
return
}
fmt.Println("Structure written into file successfully")
err = file.Close()
if err != nil {
fmt.Println(err)
return
}
}
func readFromFile(path string, assign interface{}) (interface{}, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
dec := gob.NewDecoder(file)
if err = dec.Decode(&assign); err != nil {
return nil, err
} else if assign == nil {
fmt.Println("Structure read is nil...")
return assign, nil
} else {
fmt.Println(assign)
fmt.Println("Structure read from file successfully...")
}
return assign, nil
}
// REMEMBER: BINARY NUMBERS READ RIGHT TO LEFT!!!
func displayRaw(layer int) {
for i := 0; i < len(sim[layer]); i++ {
fmt.Printf("%064d", strconv.FormatInt(int64(sim[layer][i]), 2))
fmt.Println()
}
}
func displayFancy() {
if sim == nil {
fmt.Println("Array Empty!")
}
for i := 0; i < len(sim); i++ {
for j := 0; j < 64; j++ {
for k := 0; k < len(sim[i]); k++ {
if getBit(sim[i][k], j) == 0 {
fmt.Print("□")
} else {
fmt.Print("■")
}
}
}
fmt.Println()
}
fmt.Println()
}
func setBit(n uint64, pos uint64) uint64 {
n |= 1 << pos
return n
}
func clearBit(n uint64, pos uint64) uint64 {
mask := ^(1 << pos)
n &= uint64(mask)
return n
}
func getBit(n uint64, pos int) int {
val := n & (1 << pos)
if val > 0 {
return 1
}
return 0
}
func PrintMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}