-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuts.js
165 lines (137 loc) · 4.27 KB
/
uts.js
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
/** Universal Tag System */
// TM to simulate: (move right, count cells from 1; flip odd cells, if an even cell is 1, keep it and continue, halt otherwise)
//
// Examples:
// 0 => 10
// 1 => 11
// 10 => 00
// 11 => 110
// 111 => 1011
// 1001 => 10101
// 11011 => 11110
//
// (q1,0,1,>,q2), (q1,1,0,>,q2), (q2,1,1,>,q1)
//
// -- 0,1,R -->
// -- 1,0,R -->
// (Q1) <-- 1,1,R -- (Q2) -- 0 --> (H)
//
const deletion = 2
const productions = {
// TRANSITION Q1->Q2
// move right: adjust the m and n parts
"A10": ["A'10", "x", "a'10", "x"], // +1 (write symbol is 1 for read symbol 0)
"a10": ["a'10", "x", "a'10", "x"], // *2
"A11": ["A'11", "x"], // +0 (write symbol is 0 for read symbol 1)
"a11": ["a'11", "x", "a'11", "x"], // *2
"B10": ["B'10"],
"b10": ["b'10"], // /2
"B11": ["B'11"],
"b11": ["b'11"], // /2
// prepare for the new read symbol in m and n
"A'10": ["A''21", "A''20"],
"a'10": ["a''21", "a''20"],
"A'11": ["A''21", "A''20"],
"a'11": ["a''21", "a''20"],
"B'10": ["B''21", "B''20"],
"b'10": ["b''21", "b''20"],
"B'11": ["B''21", "B''20"],
"b'11": ["b''21", "b''20"],
// transform into the new state with the new read symbol
"A''20":["x", "A20", "x"], // pad for even n (odd length of bs)
"a''20": ["a20", "x"],
"A''21": ["A21", "x"],
"a''21": ["a21", "x"],
"B''20": ["B20", "x"],
"b''20": ["b20", "x"],
"B''21": ["B21", "x"],
"b''21": ["b21", "x"],
// TRANSITION Q2->Q1
// move right: adjust the m and n parts
"A20": ["H"], // halt (no transition from from Q2 for read symbol 0)
"a20": ["a"],
"A21": ["A'21", "x", "a'21", "x"], // +1 (write symbol is 1 for read symbol 1)
"a21": ["a'21", "x", "a'21", "x"], // *2
"B20": [],
"b20": ["b"],
"B21": ["B'21"],
"b21": ["b'21"], // /2
// prepare for the new read symbol in m and n
"A'20": ["A''11", "A''10"],
"a'20": ["a''11", "a''10"],
"A'21": ["A''11", "A''10"],
"a'21": ["a''11", "a''10"],
"B'20": ["B''11", "B''10"],
"b'20": ["b''11", "b''10"],
"B'21": ["B''11", "B''10"],
"b'21": ["b''11", "b''10"],
// transform into the new state with the new read symbol
"A''10":["x", "A10", "x"], // pad for even n (odd number of bs)
"a''10": ["a10", "x"],
"A''11": ["A11", "x"],
"a''11": ["a11", "x"],
"B''10": ["B10", "x"],
"b''10": ["b10", "x"],
"B''11": ["B11", "x"],
"b''11": ["b11", "x"],
}
// 0 0 1
let word = [
'A10', 'x',
'B10', 'x', 'b10', 'x',
]
// // 0 0 0
// let word = [
// 'A10', 'x',
// 'B10', 'x',
// ]
// // 1 0 1
// let word = [
// 'A10', 'x', 'a10', 'x',
// 'B10', 'x', 'b10', 'x',
// ]
// // 0 0 11
// let word = [
// 'A10', 'x', // m = 0
// 'B10', 'x', 'b10', 'x', 'b10', 'x', 'b10', 'x' // 11 = 3 = n
// ]
// // 11 0 11
// let word = [
// 'A10', 'x', 'a10', 'x', 'a10', 'x', 'a10', 'x', // 11 = 3 m
// 'B10', 'x', 'b10', 'x', 'b10', 'x', 'b10', 'x' // 11 = 3 = n
// ]
// // 1 0 01
// let word = [
// 'A10', 'x', 'a10', 'x',
// 'B10', 'x', 'b10', 'x', 'b10', 'x' // 10 = 2 = n
// ]
// // 1 0 101
// let word = [
// 'A10', 'x', 'a10', 'x',
// 'B10', 'x', 'b10', 'x', 'b10', 'x', 'b10', 'x', 'b10', 'x', 'b10', 'x' // 101 = 5 = n
// ]
// // 0 1 0
// let word = [
// 'A11', 'x',
// 'B11', 'x',
// ]
// // 0 1 1
// let word = [
// 'A11', 'x',
// 'B11', 'x', 'b11', 'x'
// ]
// // 1 1 1
// let word = [
// 'A11', 'x', 'a11', 'x',
// 'B11', 'x', 'b11', 'x'
// ]
console.log(word.join(' '))
let t = 0
let padding = ''
let s
while ((s = word[0]) != 'H' && word.length >= deletion && t++ < 1000/*max steps*/) {
padding += word.splice(0, deletion).reduce((a,c) => a += ' '.repeat(c.length + 1), '')
word.push(...productions[s])
if (word[0].startsWith("A") || word[0].startsWith("H")) padding = ''
console.log(padding + word.join(' '))
}