-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtm.go
More file actions
165 lines (119 loc) · 3.01 KB
/
tm.go
File metadata and controls
165 lines (119 loc) · 3.01 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
package bbchallenge
import (
"encoding/binary"
"errors"
"strconv"
tabulate "github.com/rgeoghegan/tabulate"
)
const R = 0
const L = 1
type TM [2 * 5 * 3]byte
func tmTransitionToStr(b1 byte, b2 byte, b3 byte) (toRet string) {
if b3 == 0 {
return "???"
}
toRet = strconv.Itoa(int(b1))
if b2 == 0 {
toRet += "R"
} else {
toRet += "L"
}
toRet += string(rune(int('A') + int(b3) - 1))
return toRet
}
func (tm TM) ToAsciiTable(nbStates byte) (toRet string) {
var table [][]string
for i := byte(0); i < nbStates; i += 1 {
table = append(table, []string{string(rune(int('A') + int(i))),
tmTransitionToStr(tm[6*i], tm[6*i+1], tm[6*i+2]),
tmTransitionToStr(tm[6*i+3], tm[6*i+4], tm[6*i+5])})
}
layout := &tabulate.Layout{Headers: []string{"-", "0", "1"}, Format: tabulate.SimpleFormat}
asText, _ := tabulate.Tabulate(
table, layout,
)
return asText
}
func GetMachineI(db []byte, i int, hasHeader bool) (tm TM, err error) {
if i < 0 || i > len(db)/30 {
err := errors.New("invalid db index")
return tm, err
}
offset := 0
if hasHeader {
offset = 1
}
copy(tm[:], db[30*(i+offset):30*(i+offset+1)])
return tm, nil
}
func GetMachineIFromIndex(db []byte, i int, hasHeader bool, undecidedMachinesIndex []byte) (tm TM, indexInDb uint32, err error) {
if i < 0 || i > len(undecidedMachinesIndex)/4 {
err := errors.New("invalid index of undecided machines index")
return tm, 0, err
}
indexInDb = binary.BigEndian.Uint32(undecidedMachinesIndex[i*4 : (i+1)*4])
if indexInDb < 0 || indexInDb > uint32(len(db)/30) {
err := errors.New("invalid db index")
return tm, 0, err
}
offset := uint32(0)
if hasHeader {
offset = 1
}
copy(tm[:], db[30*(indexInDb+offset):30*(indexInDb+offset+1)])
return tm, indexInDb, nil
}
func GetBB5Winner() TM {
// +---+-----+-----+
// | - | 0 | 1 |
// +---+-----+-----+
// | A | 1RB | 1LC |
// | B | 1RC | 1RB |
// | C | 1RD | 0LE |
// | D | 1LA | 1LD |
// | E | 1RH | 0LA |
// +---+-----+-----+
return TM{
1, R, 2, 1, L, 3,
1, R, 3, 1, R, 2,
1, R, 4, 0, L, 5,
1, L, 1, 1, L, 4,
1, R, 0, 0, L, 1}
}
const MAX_MEMORY = 40000
type SimpleTape struct {
tape [MAX_MEMORY]byte
}
func TmStep(tm TM, read byte, currState byte, currPos int, currTime int) (write byte, nextState byte, nextPos int) {
tmTransition := 6*(currState-1) + 3*read
write = tm[tmTransition]
move := tm[tmTransition+1]
nextState = tm[tmTransition+2]
if move == R {
nextPos = currPos + 1
} else {
nextPos = currPos - 1
}
return write, nextState, nextPos
}
func TmSimulate(tm TM) (int, error) {
currPos := MAX_MEMORY / 2
nextPos := currPos
currState := byte(1)
currTime := 0
write := byte(0)
var tape SimpleTape
var err error
for err == nil && currState != 0 {
if currPos < 0 || currPos >= len(tape.tape) {
err = errors.New("memory exceeded")
continue
}
read := tape.tape[currPos]
write, currState, nextPos = TmStep(tm, read, currState, currPos, currTime)
tape.tape[currPos] = write
currPos = nextPos
currTime += 1
}
return currTime, err
}