-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrainfuck.py
56 lines (54 loc) · 1.74 KB
/
Brainfuck.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
def brain_luck(code, input):
output = ''
arr = [0]
arr_pos = 0
arr_size = 1
in_pos = 0
command_pos = 0
while command_pos < len(code):
command = code[command_pos]
if command == '>':
arr_pos += 1
if arr_pos == arr_size:
arr.append(0)
arr_size += 1
elif command == '<':
arr_pos -= 1
elif command == '+':
arr[arr_pos] = (arr[arr_pos] + 1) % 256
elif command == '-':
arr[arr_pos] = (arr[arr_pos] + 255) % 256
elif command == '.':
output += chr(arr[arr_pos])
elif command == ',':
arr[arr_pos] = ord(input[in_pos])
in_pos += 1
elif command == '[':
if arr[arr_pos] == 0:
count = 0
while command_pos < len(code):
command_pos += 1
command = code[command_pos]
if command == '[':
count += 1
elif command == ']':
if count == 0:
break
else:
count -= 1
elif command == ']':
if not arr[arr_pos] == 0:
count = 0
while command_pos >= 0:
command_pos -= 1
command = code[command_pos]
if command == ']':
count += 1
elif command == '[':
if count == 0:
break
else:
count -= 1
command_pos += 1
return output
print((brain_luck(",+[-.,+]", "Codewars" + chr(255))))