-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMJr.py
205 lines (177 loc) · 5.59 KB
/
MJr.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
import array
import random
# MJr runtime version 0 is unstable!
VERSION = 0
def int32(x):
return ((x & 0xFFFFFFFF) ^ 0x80000000) - 0x80000000
def int_ctz(x):
return (x & -x).bit_length() - 1
def hex_to_arr(typecode, chars_per_element, s):
return array.array(typecode, [
int(s[i:i + chars_per_element], 16)
for i in range(0, len(s), chars_per_element)
])
class DefaultPRNG:
def nextDouble(self):
return random.random()
def nextInt(self, n):
return random.randint(0, n - 1)
SAMPLE_EMPTY_MESSAGE = 'sample from empty range';
def nextIntChecked(rng, n):
if n <= 0:
raise ValueError(SAMPLE_EMPTY_MESSAGE);
return rng.nextInt(n)
def lfsrFeedbackTerm(n):
# http://users.ece.cmu.edu/~koopman/lfsr/
if n < 0xFF:
return 0xA6
elif n < 0x3FF:
return 0x344
elif n < 0xFFF:
return 0xAF5
elif n < 0x3FFF:
return 0x243F
elif n < 0xFFFF:
return 0x8580
elif n < 0x3FFFF:
return 0x204C9
elif n < 0xFFFFF:
return 0x80534
elif n < 0x3FFFFF:
return 0x200634
elif n < 0xFFFFFF:
return 0x8009F8
elif n < 0x3FFFFFF:
return 0x20006B9
elif n < 0xFFFFFFF:
return 0x8000893
else:
return 0x20000A46
class Grid:
def __init__(self, width, height, data, alphabet):
self.width = width
self.height = height
self.data = data
self.alphabet = alphabet
def index(self, x, y):
if x < 0 or x >= self.width or y < 0 or y >= self.height:
raise ValueError(f'position out of bounds: ({x}, {y})');
return x + y * self.width
def wrapIndex(self, x, y):
return (x % self.width) + (y % self.height) * self.width;
def __str__(self):
return '\n'.join([
''.join([
self.alphabet[self.data[i]]
for i in range(j, j + self.width)
])
for j in range(0, self.width * self.height, self.width)
])
class Pattern:
def __init__(self, width, height, pattern):
self.width = width
self.height = height
self.pattern = pattern
minX = width; minY = height; maxX = 0; maxY = 0
v = []
for y in range(height):
for x in range(width):
c = pattern[x + width * y]
if c >= 128:
continue
v.extend((x, y, c));
minX = min(minX, x);
minY = min(minY, y);
maxX = max(maxX, x + 1);
maxY = max(maxY, y + 1);
self.vectorData = v
self.minX = minX
self.minY = minY
self.effectiveWidth = max(maxX - minX, 0)
self.effectiveHeight = max(maxY - minY, 0)
def fitsMask(self, grid, mask, atX, atY):
v = self.vectorData
for i in range(0, len(v), 3):
index = grid.wrapIndex(atX + v[i], atY + v[i + 1])
if (mask[index >> 5] & 1 << (index & 31)) != 0:
return False
return True
def hasEffect(self, grid, atX, atY):
g = grid.data; v = self.vectorData
for i in range(0, len(v), 3):
index = grid.wrapIndex(atX + v[i], atY + v[i + 1])
if g[index] != v[i + 2]:
return True
return False
def put(self, grid, mask, atX, atY):
g = grid.data; v = self.vectorData
for i in range(0, len(v), 3):
index = grid.wrapIndex(atX + v[i], atY + v[i + 1])
g[index] = v[i + 2]
if mask is not None:
mask[index >> 5] |= 1 << (index & 31)
class RewriteInfo:
def __init__(self, grid, x, y, width, height):
self.grid = grid
self.x = x
self.y = y
self.width = width
self.height = height
class Sampler:
def __init__(self, domainSize):
self.count = 0
self.arr = array.array('L', range(domainSize))
self.indices = array.array('L', range(domainSize))
def copyInto(self, out):
out[:self.count] = self.arr[:self.count]
def copyIntoOffset(self, out, offset, m, c):
arr = self.arr
for i in range(self.count):
out[offset + i] = m * arr[i] + c
def shuffleInto(self, out, rng):
arr = self.arr
for i in range(self.count):
j = rng.nextInt(i + 1)
out[i] = out[j]
out[j] = arr[i]
def shuffleIntoOffset(self, out, offset, m, c, rng):
arr = self.arr
for i in range(self.count):
j = rng.nextInt(offset + i + 1)
out[offset + i] = out[j]
out[j] = m * arr[i] + c
def has(self, x):
return self.indices[x] < self.count;
def add(self, x):
arr = self.arr; indices = self.indices
i = indices[x]
if i >= self.count:
j = self.count
y = arr[j]
arr[j] = x
indices[x] = j
arr[i] = y
indices[y] = i
self.count += 1
def del_(self, x):
arr = self.arr; indices = self.indices
i = indices[x]
if i < self.count:
j = self.count - 1
y = arr[j]
arr[j] = x
indices[x] = j
arr[i] = y
indices[y] = i
self.count -= 1
def sample(self, max_, rng):
arr = self.arr; indices = self.indices
i = rng.nextInt(max_)
j = max_ - 1
x = arr[i]
y = arr[j]
arr[i] = y
indices[y] = i
arr[j] = x
indices[x] = j
return x