-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodingame.py
62 lines (54 loc) · 1.62 KB
/
codingame.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
# solution to the brainfuck puzzle on codingame.com
# https://www.codingame.com/training/medium/what-the-brainfuck
lines, s, inputcount = map(int,input().split())
array = [0] * s
codepointer = 0
arraypointer = 0
dirtycode = ""
for i in range(lines):
line = input()
dirtycode += line
inputs = []
for i in range(inputcount):
inp = int(input())
inputs.append(inp)
code = ""
for c in dirtycode:
if c in list("+-><.,[]"):
code += c
if code.count("[") != code.count("]"): print("SYNTAX ERROR"); exit()
stack = []
jump = [None] * len(code)
for i,c in enumerate(code):
if c == "[":
stack.append(i)
elif c == "]":
jump[i] = stack.pop()
jump[jump[i]] = i
while codepointer < len(code):
command = code[codepointer]
if command == ">":
arraypointer += 1
if arraypointer >= s:
print("POINTER OUT OF BOUNDS"); exit()
elif command == "<":
arraypointer -= 1
if arraypointer < 0:
print("POINTER OUT OF BOUNDS"); exit()
elif command == "+":
array[arraypointer] += 1
if array[arraypointer] > 255:
print("INCORRECT VALUE"); exit()
elif command == "-":
array[arraypointer] -= 1
if array[arraypointer] < 0:
print("INCORRECT VALUE"); exit()
elif command == ".":
print(chr(array[arraypointer]),end="")
elif command == ",":
array[arraypointer] = inputs.pop(0)
elif command == "[" and not array[arraypointer]:
codepointer = jump[codepointer]
elif command == "]" and array[arraypointer]:
codepointer = jump[codepointer]
codepointer += 1