-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbefunk.py
372 lines (358 loc) · 12.2 KB
/
befunk.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
from PIL import Image
import sys
import random
instrDict = dict((i, str(hex(i))[2:]) for i in range(16))
instrDict.update({50: '>',
51: '^',
52: 'v',
53: '<',
54: '_',
55: '|',
56: '[',
57: ']',
58: 'x',
59: 'w',
100: ',',
101: '.',
102: '&',
103: '~',
150: '+',
151: '-',
152: '*',
153: '/',
154: '%',
175: ':',
176: '\\',
200: '#',
201: 'j',
202: 'k',
256: '"',
300: 'g',
301: 'p',
302: "'",
303: 's',
350: '!',
351: '`',
400: '{',
401: '}',
402: 'u',
500: 'n',
501: '$',
554: 'z',
999: '@'})
delta = [0, 1]
x = 0
y = 0
stackstack = [[]]
stringmode = False
skipcounter = 0
origin = [0, 0]
storeoffset = [0, 0]
def move(x, y):
global m, delta
if x + delta[1] not in range(0, len(m[y])) or y + delta[0] not in range(0, len(m)):
delta = [-x for x in delta]
while x + delta[1] in range(0, len(m[y])) and y + delta[0] in range(0, len(m[y])):
x += delta[1]
y += delta[0]
delta = [-x for x in delta]
else:
x += delta[1]
y += delta[0]
return x, y
def push(x):
stackstack[-1].append(x)
def pop():
return stackstack[-1].pop() if stackstack[-1] != [] else 0
def exec(num):
global y, x, delta, stringmode, skipcounter, storeoffset, origin, m
if num == 555 and stringmode:
push(555)
while num == 555:
x, y = move(x, y)
num = m[y][x]
if num == 556:
x, y = move(x, y)
num = m[y][x]
while num != 556:
x, y = move(x, y)
num = m[y][x]
if not stringmode:
if num in range(16):
push(num)
elif num == 50:
delta = [0, 1]
elif num == 51:
delta = [-1, 0]
elif num == 52:
delta = [1, 0]
elif num == 53:
delta = [0, -1]
elif num == 54:
delta = [0, 1] if pop() == 0 else [0, -1]
elif num == 55:
delta = [1, 0] if pop() == 0 else [-1, 0]
elif num == 56:
delta = [-delta[1], delta[0]]
elif num == 57:
delta = [delta[1], -delta[0]]
elif num == 58:
delta = [pop(), pop()]
elif num == 59:
a, b = pop(), pop()
delta = [-delta[1], delta[0]] if a < b else [delta[1], -delta[0]] if a > b else delta
elif num == 60:
delta = random.choice([[0, 1], [0, -1], [1, 0], [-1, 0]])
elif num == 100:
print(chr(pop() % 256), end="")
elif num == 101:
print(pop(), end="")
elif num == 102:
good = False
while not good:
a = input()
intstr = ""
for i in range(len(a)):
if a[i] in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
intstr += a[i]
i += 1
while i < len(a) and a[i] in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
intstr += a[i]
i += 1
break
try:
push(int(intstr))
good = True
except:
pass
elif num == 103:
try:
for i in input():
push(ord(i))
except:
push(10)
elif num == 150:
push(pop() + pop())
elif num == 151:
a, b = pop(), pop()
push(b - a)
elif num == 152:
push(pop() * pop())
elif num == 153:
a, b = pop(), pop()
push(0 if a == 0 else b // a)
elif num == 154:
a, b = pop(), pop()
push(0 if a == 0 else b % a)
elif num == 175:
a = pop()
push(a); push(a)
elif num == 176:
a, b = pop(), pop()
push(b); push(a)
elif num == 200:
skipcounter += 1
elif num == 201:
skipcounter += pop()
elif num == 202:
a = pop()
if a > 0:
i, j = move(x, y)
while m[j][i] == 555:
i, j = move(i, j)
for k in range(a):
exec(m[j][i])
else:
skipcounter += 1
elif num == 256:
stringmode = True
elif num == 300:
try: push(m[pop() + storeoffset[0] + origin[0]][pop() + storeoffset[1] + origin[1]])
except IndexError: push(555)
elif num == 301:
a, b, c = pop(), pop(), pop()
if a + storeoffset[0] + origin[0] >= 0 and b + storeoffset[1] + origin[1] >= 0:
good = False
while not good:
try:
m[a + storeoffset[0] + origin[0]][b + storeoffset[1] + origin[1]] = c
good = True
except IndexError:
while a + storeoffset[0] + origin[0] >= len(m):
m.append([555 for i in range(len(m[0]))])
while b + storeoffset[1] + origin[1] >= len(m[a]):
for i in range(len(m)):
m[i].append(555)
else:
while a + storeoffset[0] + origin[0] < 0:
m = [[555 for i in range(m[0])]] + m
y += 1
origin[0] += 1
while b + storeoffset[1] + origin[1] < 0:
for i in range(len(m)):
m[i] = [555] + m[i]
x += 1
origin[1] += 1
m[a + storeoffset[0] + origin[0]][b + storeoffset[1] + origin[1]] = c
elif num == 302:
i, j = x + delta[1], y + delta[0]
try: push(m[j][i])
except IndexError: push(555)
elif num == 303:
a = pop()
try:
m[y+delta[0]][x+delta[1]] = a
except IndexError:
while y+delta[0] >= len(m):
m.append([0 for x in m[555]])
while x+delta[1] >= len(m[y]):
m[y+delta[0]].append(555)
m[y+delta[0]][x+delta[1]] = a
for i in range(len(m)):
m[i] = [(m[i][x] if x < len(m[i]) else 555) for x in range(max([len(k) for k in m]))]
elif num == 350:
push(int(not pop()))
elif num == 351:
push(int(pop() < pop()))
elif num == 400:
a = pop()
try:
stackstack.append([])
if a > 0:
stackstack[-1] = stackstack[-2][-a:len(stackstack[-2])] if len(stackstack[-2]) > a else [0 for x in range(len(stackstack[-2])-a)] + stackstack[-2]
stackstack[-2] = stackstack[-2][0:-a] if len(stackstack[-2]) > a else []
stackstack[-2].append(storeoffset[1])
stackstack[-2].append(storeoffset[0])
elif a < 0:
stackstack[-1] = []
for i in range(abs(a)):
stackstack[-1].append(0)
storeoffset[1], storeoffset[0] = move(x, y)
storeoffset[1] -= origin[1]
storeoffset[0] -= origin[0]
except MemoryError:
delta = [-i for i in delta]
elif num == 401:
if len(stackstack) > 1:
a = pop()
storeoffset = [stackstack[-2].pop() if stackstack[-2] != [] else 0, stackstack[-2].pop() if stackstack[-2] != [] else 0]
if a > 0:
stackstack[-2] += stackstack[-1][-a:] if len(stackstack[-1]) > a else [0 for x in range(a - len(stackstack[-1]))] + stackstack[-1]
stackstack.pop()
elif a < 0:
for i in range(abs(a)):
stackstack[-2].pop()
stackstack.pop()
else:
stackstack.pop()
else:
delta = [-i for i in delta]
elif num == 402:
if len(stackstack) > 1:
a = pop()
if a > 0:
for i in range(a):
push(stackstack[-2].pop() if stackstack[-2] != [] else 0)
elif a < 0:
for i in range(abs(a)):
stackstack[-2].append(pop())
else:
delta = [-i for i in delta]
elif num == 500:
stackstack[-1].clear()
elif num == 501:
pop()
elif num == 554:
pass
elif num == 998:
sys.exit(pop())
elif num == 999:
sys.exit(0)
else:
delta = [-x for x in delta]
else:
if num == 256:
stringmode = False
else:
push(num % 256)
def execute(f):
global m, delta, debug, x, y, skipcounter
i = Image.open(f)
l = i.load()
m = []
for j in range(i.size[0]):
"""This here converts the PNG file into a two-dimensional list of least significant base-10 digits
of every channel of every pixel."""
list_ = []
for k in range(i.size[1]):
count = 2
count2 = 0
for n in l[j, k]:
count2 += ((n % 10) * 10 ** count)
count -= 1
list_.append(int(count2))
m.append(list_)
m = [list(i) for i in zip(*m)] # invert the axes of m, so it's the list of rows and not columns.
i.close()
for i in range(len(m)-1, -1, -1):
if all(x == 555 for x in m[i]):
m.pop()
else: break
l = [list(i) for i in zip(*m)]
for i in range(len(l)-1, -1, -1):
if all(x == 555 for x in l[i]):
l.pop()
else: break
m = [list(i) for i in zip(*l)]
print(u"Befunge program (inaccurate): \n{0}\n".format(
"\n".join(["".join([(instrDict[m[y][x]] if m[y][x] in instrDict else " ")
for x in range(len(m[y]))]) for y in range(len(m))])))
while True:
while skipcounter > 0:
x, y = move(x, y)
skipcounter -= 1
delta = [-x for x in delta]
while skipcounter < 0:
x, y = move(x, y)
skipcounter += 1
delta = [-x for x in delta]
exec(m[y][x])
if debug:
print("\nProgram: " + str(m))
print("Stack: ", end="")
for i in stackstack[-1]:
try: print(str(i) + " (%s)" % chr(i % 256), end=" | ")
except: print(i)
print("")
print("Number executed: " + str(m[y][x]) + (" (Befunge: %s)" % instrDict[m[y][x]] if not stringmode and m[y][x] in instrDict else "(%s)" % chr(m[y][x] % 255) if stringmode else ""))
print("X: " + str(x))
print("Y: " + str(y))
print("Delta: " + str(delta))
input()
x, y = move(x, y)
if __name__ == '__main__':
global debug
good = False
while not good:
try:
f = input("Enter Befunk filename (include file extension (usually .PNG))\n")
Image.open(f)
good = True
except:
print("Enter a valid file name.\n")
good = False
while not good:
try:
d = input("Use debug mode? Y/N\n")
if d.lower() in ['yes', 'y']:
debug = True
visual = True
else:
if d.lower() in ['no', 'n']:
debug = False
else:
raise TypeError
good = True
except:
print("Please enter 'y', 'n', 'yes', or 'no'.")
execute(f)