-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwildfire.py
More file actions
103 lines (100 loc) · 3.12 KB
/
wildfire.py
File metadata and controls
103 lines (100 loc) · 3.12 KB
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
import sys
import os
def exec(src):
grid = {}
lines = src.split('\n')
start_x, start_y = -1, -1
has_plant_command = False
for y, line in enumerate(lines):
line = line.strip('\r')
if '#' in line:
line = line[:line.index('#')]
for x, char in enumerate(line):
grid[(x, y)] = char
if char == '@':
start_x, start_y = x, y
if char == 'P':
has_plant_command = True
if not grid:
return
if start_x == -1:
sys.stderr.write("Error: Starting point '@' not found.\n")
sys.exit(1)
if not has_plant_command:
sys.stderr.write("Error: Rule Violation. No 'P' command found in source. Execution denied.\n")
sys.exit(1)
memory = [0] * 30000
pointer = 0
dx, dy = 1, 0
x, y = start_x, start_y
has_planted_at_runtime = False
while True:
char = grid.get((x, y), ' ')
if char == ' ':
break
grid[(x, y)] = ' '
if char == 'T':
memory[pointer] = (memory[pointer] + 1) % 1114112
elif char == 't':
memory[pointer] = (memory[pointer] - 1) % 1114112
elif char == '}':
pointer += 1
elif char == '{':
pointer = max(0, pointer - 1)
elif char == '>':
dx, dy = 1, 0
elif char == '<':
dx, dy = -1, 0
elif char == '^':
dx, dy = 0, -1
elif char == 'v':
dx, dy = 0, 1
elif char == '.':
sys.stdout.write(chr(memory[pointer]))
sys.stdout.flush()
elif char == ',':
ch = sys.stdin.read(1)
memory[pointer] = ord(ch) if ch else 0
elif char == '?':
if memory[pointer] == 0:
x += dx
y += dy
elif char == 'P':
has_planted_at_runtime = True
nx, ny = x + dx, y + dy
grid[(nx, ny)] = chr(memory[pointer])
x += dx
y += dy
if not has_planted_at_runtime:
sys.stderr.write("\nError: Rule Violation. The 'P' command was not executed during runtime. Forest died.\n")
sys.exit(1)
def idle():
sys.stdout.write("Wildfire IDLE (Enter 'RUN' to execute, 'EXIT' to quit)\n")
while True:
sys.stdout.write("WF> ")
sys.stdout.flush()
lines = []
while True:
try:
line = input()
except EOFError:
return
if line.strip() == "EXIT":
return
if line.strip() == "RUN":
break
lines.append(line)
if lines:
source = '\n'.join(lines)
exec(source)
sys.stdout.write("\n")
if __name__ == "__main__":
if len(sys.argv) > 1:
filepath = sys.argv[1]
if os.path.exists(filepath):
with open(filepath, 'r', encoding='utf-8') as f:
exec(f.read())
else:
sys.stdout.write("File not found.\n")
else:
idle()