Removing files from data analysis directory
[ou-summer-of-code-2017.git] / 02-lifts / bf2.py
1 import sys
2
3
4 program = '>' + '+' * 61 + '>' + '+' * 33 + '>' + '+' * 24
5 program += """
6
7
8 read character into cell 9
9 >>>>>>,
10
11
12
13 while cell 9 != 0 # have an input
14 [
15 set cell 10 to 1
16 >[-]+
17
18 # clear cells 4 and 0
19 <<<<<<[-]<<<<[-]>
20 # copy cell 1 to cell 0 using cell 4
21 [-<+>>>>+<<<]
22 >>>[-<<<+>>>]
23 <<<<
24
25 subtract cell 0 from cell 9
26 [->>>>>>>>>-<<<<<<<<<]
27 >>>>>>>>>
28
29 while cell 9 != 0 # we're not at an exit
30 [
31 set cell 10 to 0
32 >[-]
33
34 copy cell 2 to cell 0 using cell 4
35 <<<<<<<<[-<<+>>>>+<<]
36 >>[-<<+>>]
37 <<<<
38
39 subtract cell 0 from cell 9
40 [->>>>>>>>>-<<<<<<<<<]
41 >>>>>
42
43 set cell 5 to 1
44 [-]+
45 >>>>
46
47
48 while cell 9 != 0 # we're going down
49 [
50 clear cell 5
51 <<<<[-]
52 copy cell 7 to cell 11 using cell 12
53 >>[->>>>+>+<<<<<]
54 >>>>>[-<<<<<+>>>>>]
55
56 cell 12 is zero
57
58 while cell 11 != 0 # above ground
59 <
60 [
61 set cell 12 to 1
62 >[-]+
63
64 clear cell 11
65 <[-]
66 end
67 ] 11
68
69 while cell 12 != 0
70 >
71 [
72 decrement cell 7
73 <<<<<-
74
75 set cell 12 to zero
76 >>>>>[-]
77 end
78 ] 12
79
80 <<<<<<
81 decrement cell 6
82 -
83
84 have now dealt with the input so clear cell 9
85 >>>[-]
86 end
87 ] 9
88
89
90 while cell 5 != 0 # we're going up
91 <<<<
92 [
93 clear cell 5
94 [-]
95
96
97 # set cell 12 to 0
98 >>>>>>>[-]
99
100 ### if 6 == 0 or 7 != 0
101 ### set cell 12 to 1
102
103 # copy cell 6 to cell 11 using cell 12
104 <<<<<<[->>>>>+>+<<<<<<]
105 >>>>>>[-<<<<<<+>>>>>>]
106
107 set cell 12 to 1
108 [-]+
109
110 while cell 11 != 0
111 <
112 [
113 clear cell 12
114 >[-]
115
116 set cell 11 to 0
117 <[-]
118 end 11
119 ] 11
120
121
122
123 # copy cell 7 to cell 11 using cell 13
124 <<<<[->>>>+>>+<<<<<<]
125 >>>>>>[-<<<<<<+>>>>>>]
126
127 # while cell 11 != 0
128 <<
129 [
130 set cell 12 to 1
131 >[-]+
132
133 set cell 11 to 0
134 <[-]
135
136 # end 11
137 ] 11
138 # add cell 12 to cell 7
139 >[-<<<<<+>>>>>]
140
141 # increment cell 6
142 <<<<<<+
143 <
144 end
145 ] 5
146
147 have now dealt with the non exit node
148 clear cell 9
149 >>>>
150 [-]
151 end
152 ] 9
153
154 while cell 10 != 0 # at an exit
155 >
156 [
157 copy cell 7 to cell 12 using cell 13 (highest)
158 <<<[->>>>>+>+<<<<<<]
159 >>>>>>[-<<<<<<+>>>>>>]
160
161 while cell 12 != 0 (above ground level)
162 <
163 [
164 copy cell 8 to cell 11 using cell 13 (highest)
165 <<<<[->>>+>>+<<<<<]
166 >>>>>[-<<<<<+>>>>>]
167
168 cell 13 is zero
169
170 ### subtract 11 from 12 ensuring 12 gte 0
171 ### add 12 to 8
172
173 while cell 11 != 0
174 <<
175 [
176 copy cell 12 to cell 14 using cell 15
177 >[->>+>+<<<]
178 >>>[-<<<+>>>]
179
180 while cell 14 != 0
181 <
182 [
183 set cell 13 to 1
184 <[-]+
185 decrement cell 14
186 >-
187 end
188 ] 14
189 while cell 13 != 0
190 <
191 [
192 decrement cell 12
193 <-
194 decrement cell 13
195 >-
196 end
197 ] 13
198
199 decrement cell 11
200 <<-
201 end
202 ] 11
203
204 >[-<<<<+>>>>]
205 add cell 12 to cell 8
206 ] 12
207 <<
208 clear 10
209 [-]
210 ] 10
211
212
213
214 read character into cell 9
215 <,
216 end
217 ] 9
218
219 output cell 8
220 <.
221
222 """
223
224
225 def execute(filename):
226 f = open(filename, "r")
227 evaluate(f.read())
228 f.close()
229
230
231 def evaluate(code, inp=None, debug=False, execution_limit=0):
232 code = cleanup(list(code))
233 bracemap = buildbracemap(code)
234
235 cells, codeptr, cellptr = [0], 0, 0
236 inputptr = 0
237 execution_count = 0
238 outputs = []
239
240 try:
241 while codeptr < len(code):
242 command = code[codeptr]
243
244 if debug:
245 print(command, codeptr, cellptr, cells)
246
247 if command == ">":
248 cellptr += 1
249 if cellptr == len(cells): cells.append(0)
250
251 if command == "<":
252 cellptr = 0 if cellptr <= 0 else cellptr - 1
253
254 if command == "+":
255 cells[cellptr] = cells[cellptr] + 1 if cells[cellptr] < 255 else 0
256
257 if command == "-":
258 cells[cellptr] = cells[cellptr] - 1 if cells[cellptr] > 0 else 255
259
260 if command == "[" and cells[cellptr] == 0: codeptr = bracemap[codeptr]
261 if command == "]" and cells[cellptr] != 0: codeptr = bracemap[codeptr]
262 if command == ".":
263 print(cells[cellptr]) # sys.stdout.write(chr(cells[cellptr]))
264 outputs += [cells[cellptr]]
265 if command == ",":
266 if inputptr >= len(inp):
267 # raise EOFError
268 cells[cellptr] = 0
269 else:
270 cells[cellptr] = ord(inp[inputptr])
271 inputptr += 1
272
273 codeptr += 1
274 execution_count += 1
275 if execution_limit != 0 and execution_count > execution_limit:
276 break
277 except EOFError:
278 pass
279 print(execution_count)
280 return cells, codeptr, cellptr, outputs
281
282
283 def cleanup(code):
284 return list(filter(lambda x: x in '.,[]<>+-', code))
285
286
287 def buildbracemap(code):
288 temp_bracestack, bracemap = [], {}
289
290 for position, command in enumerate(code):
291 if command == "[": temp_bracestack.append(position)
292 if command == "]":
293 start = temp_bracestack.pop()
294 bracemap[start] = position
295 bracemap[position] = start
296 return bracemap
297
298 def main():
299
300 open('part2.bf', 'w').write(program + '\n')
301 inpt = '^=vvv=^^^=^=vv'
302 inpt = 'v^^^^^v=v='
303 inpt = open('02-lifts.txt').read().strip()
304 # cells, codeptr, cellptr, outputs = evaluate(program, inp=inpt, execution_limit=200000, debug=True)
305 # cells, codeptr, cellptr, outputs = evaluate(program, inp=inpt, debug=True)
306 cells, codeptr, cellptr, outputs = evaluate(program, inp=inpt)
307 print(cells, codeptr, cellptr, outputs)
308
309 if __name__ == "__main__": main()
310