SHOW:
|
|
- or go back to the newest paste.
1 | class State: | |
2 | def __init__(self,arr=[0],arr_i=0,code_i=0): | |
3 | self.arr = arr | |
4 | self.arr_i = arr_i | |
5 | self.code_i = code_i | |
6 | def copy(self): | |
7 | return State(self.arr[:],self.arr_i,self.code_i) | |
8 | - | def __str__(self): |
8 | + | |
9 | - | a = '['+''.join(map(str,self.arr))+']' |
9 | + | |
10 | - | p = ' '*(self.arr_i) + ' ^' + ' '*(len(self.arr) - self.arr_i + self.code_i) + ' ^' |
10 | + | |
11 | - | return a + ' ' + code + '\n' + p |
11 | + | |
12 | - | def __repr__(self): |
12 | + | |
13 | - | return 'State('+`self.arr`+','+`self.arr_i`+','+`self.code_i` |
13 | + | |
14 | else: | |
15 | state.code_i = brackets[state.code_i]+1 | |
16 | ||
17 | def while_close(state): | |
18 | state.code_i = brackets[state.code_i] | |
19 | ||
20 | def flip(state): | |
21 | state.arr[state.arr_i] = 1 - state.arr[state.arr_i] | |
22 | state.code_i += 1 | |
23 | ||
24 | def right(state): | |
25 | state.arr_i += 1 | |
26 | if state.arr_i >= len(state.arr): | |
27 | state.arr.append(0) | |
28 | state.code_i += 1 | |
29 | ||
30 | def left(state): | |
31 | if state.arr_i: | |
32 | state.arr_i -= 1 | |
33 | else: | |
34 | state.arr.insert(0,0) | |
35 | state.code_i += 1 | |
36 | ||
37 | def nd_choose(state): | |
38 | state.code_i += 1 | |
39 | c = state.copy() | |
40 | state.arr[state.arr_i] = 0 | |
41 | c.arr[c.arr_i] = 1 | |
42 | return c | |
43 | ||
44 | instructions = { | |
45 | '[':while_open, | |
46 | ']':while_close, | |
47 | '*':flip, | |
48 | '>':right, | |
49 | '<':left, | |
50 | '?':nd_choose } | |
51 | ||
52 | states = [State()] | |
53 | code = raw_input() | |
54 | ||
55 | #Find matching brackets | |
56 | brackets = [0]*len(code) | |
57 | stack = [] | |
58 | for i in range(len(code)): | |
59 | if code[i]=='[': | |
60 | stack.append(i) | |
61 | elif code[i]==']': | |
62 | b = stack.pop() | |
63 | brackets[i]=b | |
64 | brackets[b]=i | |
65 | ||
66 | while states: | |
67 | new_states = [] | |
68 | for s in states: | |
69 | c = code[s.code_i] | |
70 | n = instructions[c](s) | |
71 | if n: | |
72 | new_states.append(n) | |
73 | states.extend(new_states) | |
74 | terminated = filter(lambda s:s.code_i == len(code),states) | |
75 | if any(map(lambda s:s.arr[s.arr_i],terminated)): | |
76 | print 1 | |
77 | break | |
78 | states = filter(lambda s:s.code_i < len(code),states) | |
79 | if len(states) == 0: | |
80 | print 0 | |
81 | break |