-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemojistack2.py
executable file
·96 lines (81 loc) · 2.84 KB
/
emojistack2.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
from PIL import Image
import numpy as np
timesub = {"🕛": "0", "🕐": "1", "🕑": "2", "🕒": "3", "🕓": "4", "🕔": "5", "🕕": "6", "🕖": "7", "🕗": "8", "🕘": "9", "🕙": "A", "🕚": "B"}
def clock_to_int(clock_string):
output = ""
for clock in clock_string:
output += timesub[clock]
return int(output, 12)
def evaluate(code, init_state=None):
right = "👉"
left = "👈"
up = "👆"
down = "👇"
add = "👍"
sub = "👎"
out = "💬"
huh = "👂"
rep = "🔁"
start = "🫸"
end = "🫷"
bracemap = buildbracemap(code)
codeptr = 0
cellptr = [0, 0]
if init_state == None:
cells = [[0 for x in range(256)] for x in range(256)]
else:
cells = init_state
loopctr = 0
loop = False
while codeptr < len(code):
command = code[codeptr]
if command == right:
cellptr[1] = min(cellptr[1] + 1, 255)
elif command == left:
cellptr[1] = max(cellptr[1] - 1, 0)
elif command == up:
cellptr[0] = min(cellptr[0] + 1, 255)
elif command == down:
cellptr[0] = max(cellptr[0] - 1, 0)
elif command == add:
cells[cellptr[0]][cellptr[1]] = min(cells[cellptr[0]][cellptr[1]] + 1, 255)
elif command == sub:
cells[cellptr[0]][cellptr[1]] = max(cells[cellptr[0]][cellptr[1]] - 1, 0)
elif command == out:
print(chr(cells[cellptr[0]][cellptr[1]]), end="")
elif command == huh:
cells[cellptr[0]][cellptr[1]] = ord(input()[0])
elif command == start and cells[cellptr[0]][cellptr[1]] == 0:
codeptr = bracemap[codeptr]
elif command == end and cells[cellptr[0]][cellptr[1]] != 0:
codeptr = bracemap[codeptr]
elif command == rep:
if loopctr == 0 and not loop:
loopctr = clock_to_int(code[codeptr+1:codeptr+4])
loop = True
if loop == True and loopctr == 0:
loop = False
if loop:
codeptr -= 2
loopctr -= 1
codeptr += 1
return cells
def buildbracemap(code):
temp_bracestack, bracemap = [], {}
for position, command in enumerate(code):
if command == "🫸": temp_bracestack.append(position)
if command == "🫷":
start = temp_bracestack.pop()
bracemap[start] = position
bracemap[position] = start
return bracemap
state = Image.open("turing.png").convert('L')
state_array = []
for y in range(state.height):
tmp = []
for x in range(state.width):
tmp.append(state.getpixel((x, y)))
state_array.append(tmp)
with open("program.txt", "r") as f:
out = evaluate(f.read(), state_array)
Image.fromarray(np.asarray(out, np.uint8), "L").show()