-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
221 lines (191 loc) · 4.99 KB
/
main.py
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
import re
import random
ATOMS = {
# Bits
'a', # Low
'A', # High
'f', # Free Low
'F', # Free High
'r', # Rotating Low
'R', # Rotating High
# Actors / Borders
'c', # Compare to Low / Tail
's', # Set to Low / Head
'S', # Set to High
'<', # Advance
'|', # Axis
# Switches
'~', # Flip Free
'+', # Raise/Randomize Set
}
RULES = {
# Flow
'fa': 'af',
'Fa': 'Af',
'fA': 'aF',
'FA': 'AF',
'fc': 'cf',
'Fc': 'cF',
'f~': '~F',
'F~': '~f',
'f+': '+f',
'F+': '+F',
# Compare
'ca': 'ac',
'cA': '<A',
'cc': 'sc',
'c~': '~c',
'c+': '+c',
# Set
'as': 'sa',
'As': 'sa',
'aS': 'sA',
'AS': 'sA',
'~s': 's~',
'~S': 'S~',
'+s': 'S+',
'+S': ('s+', 'S+'),
'ss': '<S',
# Advance
'a<': '<a',
'A<': '<A',
'~<': '<~',
'+<': '<+',
's<': '<S',
# Reset
'f<': 'sf',
'F<': 'sF',
'fS': 'cf',
'FS': 'cF',
# Rotate
'f|': 'r|',
'F|': 'R|',
'|r': '|f',
'|R': '|F',
'ar': 'ra',
'aR': 'Ra',
'Ar': 'rA',
'AR': 'RA',
'fr': 'rf',
'fR': 'Rf',
'Fr': 'rF',
'FR': 'RF',
'cr': 'rc',
'cR': 'Rc',
'sr': 'rs',
'sR': 'Rs',
'Sr': 'rS',
'SR': 'RS',
'<r': 'r<',
'<R': 'R<',
'~r': 'r~',
'~R': 'R~',
'+r': 'r+',
'+R': 'R+',
}
print(f"Atoms {len(ATOMS)} Rules {len(RULES)}")
def iterchains(rules, only_targets=False):
for key, value in rules.items():
assert isinstance(key, str)
if not only_targets:
yield key
if not isinstance(value, tuple):
value = (value,)
for v in value:
assert isinstance(v, str)
yield v
assert set(a for chain in iterchains(RULES) for a in chain) == ATOMS
assert all(len(chain) == 2 for chain in iterchains(RULES))
assert all(chain not in RULES for chain in iterchains(RULES, only_targets=True))
def get_run_func(rules):
regex = re.compile((r'|').join(r'(?:' + re.escape(s) + r')' for s in rules))
def replace(m):
repl = rules[m.group()]
if isinstance(repl, tuple):
return random.choice(repl)
return repl
def step(text):
return regex.sub(replace, text)
def run(text, verbose=False):
if verbose:
print(text)
last = text
count = 0
epoch = 1
while True:
text = step(text)
if verbose:
print(text)
if text == last:
break
count += 1
if count == epoch:
last = text
epoch <<= 1
return text
return run
run = get_run_func(RULES)
def simulate(text, interactions):
start = '1000'
marker = ''
alphabet = sorted(set(text) | {c for x, y in interactions.items() for c in x + y} | {marker})
idx = 0
alpha_to_chain = {}
for a in alphabet:
while True:
candidate = f'{idx:b}'
idx += 1
if start not in candidate:
alpha_to_chain[a] = candidate
break
bitlen = max(len(chain) for chain in alpha_to_chain.values())
codelen = len(start) + bitlen
alpha_to_chain = {x: start + (bitlen - len(y)) * "0" + y for x, y in alpha_to_chain.items()}
phrase_to_chain = lambda phrase: ''.join(alpha_to_chain[c] for c in phrase)
marker_chain = alpha_to_chain[marker]
input_text = marker_chain + phrase_to_chain(text)
interaction_to_chain = lambda x, y: ('sp' +
phrase_to_chain(x).replace('0', 'a').replace('1', 'b') +
phrase_to_chain(y).replace('0', 'c').replace('1', 'd') +
't'
)
whole_text = 'y' + input_text + ''.join(interaction_to_chain(x, y) for x, y in interactions.items()) + 'z'
result = run(whole_text)
pos = result.find(start)
assert pos != -1
result = result[pos:] + result[:pos]
pos = result.find(marker_chain)
assert pos != -1
result = result[pos + len(marker_chain):] + result[:pos]
chain_to_alpha = {y: x for x, y in alpha_to_chain.items()}
assert (len(result) % codelen) == 0
pos = 0
output = []
while pos < len(result):
output.append(chain_to_alpha[result[pos:pos + codelen]])
pos += codelen
output = ''.join(output)
prnt(output)
return output
assert run('sca+c') == '<SA+c'
assert run('f<SA+c') == '<SA+cF'
assert run('fsca+c') == '<SA+cF'
assert run('fffffffsca+cfffffffffff') == '<SA+cFFFFFFFfffffffffff'
# FffFfffF => fFFffFfF
assert run('fffFffffFffFfffFffFffffFFfsc~A~+a+a+~A~+aa+a~A~c', verbose=True) == '<S~A~+a+a+~a~+aa+a~A~cfFFffFfFffFffffFFfffffffff'
# TODO fix simulate
"""
assert simulate("I love my life", interactions={"life": "lie", "my lie": "his lie", "love his": "love their"}) == 'I love their lie'
assert simulate("+0100101101=", interactions={
'+00': '0+',
'+01': '1+',
'+10': '1+',
'+11': '1p',
'p00': '1+',
'p01': '0p',
'p10': '0p',
'p11': '1p',
'+=': '',
'p=': '1',
}) == '101101'
"""