SHARE
TWEET

whatfuck.py

a guest Dec 26th, 2012 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.                
  9. #instructions
  10. #Return a new state if one is created, otherwise return None
  11. def while_open(state):
  12.         if state.arr[state.arr_i]:
  13.                 state.code_i += 1
  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
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top