-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeopt.py
136 lines (120 loc) · 4.03 KB
/
deopt.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
import random
from builder import *
from config import *
class Simplifier(Builder):
def __init__(self, path):
self.load(path)
def remove_nops(self):
nop = set(C0)
for i in range(self.height):
for j in range(self.width):
if self.map[i][j] in nop:
self.map[i][j] = ' '
def remove_deadcode(self):
assert self.map[0][0] == '>'
q = [(0, 0, 2)]
reachable = {q[0]}
dx, dy = 0, 0
h = 0
x, y = 0, 0
valid = lambda _x, _y, _d: 0 <= _x < self.width and 0 <= _y < self.height
while h < len(q):
# print(q[h])
x, y, d = q[h]
h += 1
c = self.map[y][x]
if c == '|':
v = x, y - 1, 3
if valid(*v) and v not in reachable:
reachable.add(v)
q.append(v)
v = x, y + 1, 1
if valid(*v) and v not in reachable:
reachable.add(v)
q.append(v)
elif c == '_':
v = x - 1, y, 0
if valid(*v) and v not in reachable:
reachable.add(v)
q.append(v)
v = x + 1, y, 2
if valid(*v) and v not in reachable:
reachable.add(v)
q.append(v)
elif c != '@':
_d = '<v>^'.find(self.map[y][x])
if _d != -1:
# new direction
d = _d
else:
# keep moving
pass
dx, dy = [[-1, 0], [0, 1], [1, 0], [0, -1]][d]
v = x + dx, y + dy, d
if valid(*v) and v not in reachable:
reachable.add(v)
q.append(v)
uniq = {(x, y) for x, y, _ in reachable}
print('visisted %d cell, %d unique cell' % (len(q), len(uniq)))
for y in range(self.height):
for x in range(self.width):
if (x, y) not in uniq:
self.map[y][x] = ' '
def build_bridge(self):
assert self.map[0][0] == '>'
q = [(0, 0, 2)]
reachable = {q[0]}
dx, dy = 0, 0
h = 0
x, y = 0, 0
valid = lambda _x, _y, _d: 0 <= _x < self.width and 0 <= _y < self.height
while h < len(q):
# print(q[h])
x, y, d = q[h]
h += 1
c = self.map[y][x]
if c == '|':
v = x, y - 1, 3
if valid(*v) and v not in reachable:
reachable.add(v)
q.append(v)
v = x, y + 1, 1
if valid(*v) and v not in reachable:
reachable.add(v)
q.append(v)
elif c == '_':
v = x - 1, y, 0
if valid(*v) and v not in reachable:
reachable.add(v)
q.append(v)
v = x + 1, y, 2
if valid(*v) and v not in reachable:
reachable.add(v)
q.append(v)
elif c != '@':
_d = '<v>^'.find(self.map[y][x])
if _d != -1:
# new direction
d = _d
elif c == ' ':
self.map[y][x] = '#'
dx, dy = [[-1, 0], [0, 1], [1, 0], [0, -1]][d]
v = x + dx, y + dy, d
if valid(*v) and v not in reachable:
reachable.add(v)
q.append(v)
if __name__ == '__main__':
builder = Simplifier('map')
print(builder.show())
builder.remove_nops()
builder.save('map_simple')
builder.remove_deadcode()
builder.save('map_better')
builder = Simplifier('map_simple')
# cheat
assert builder.map[80][89] == '<'
builder.map[80][89] = 'v'
builder.remove_deadcode()
builder.save('map_best')
builder.build_bridge()
builder.save('map_final')