fork download
  1. from __future__ import print_function
  2. from collections import deque
  3. try:
  4. input = raw_input
  5. range = xrange
  6. except NameError:
  7. pass
  8.  
  9. """
  10. aaaa ..... n NOP - no operation; do nothing
  11. aaab ..... i Input - push input onto top of stack
  12. aaba ..... > Rot - pops top stack element and pushes to bottom of stack
  13. aabb ..... \ Swap - swaps top two stack elements
  14. aabc ..... 1 Push - pushes a 1 onto the top of stack (creates new element)
  15. abaa ..... < RRot - pops bottom element and pushes to top of stack
  16. abab ..... d Dup - Duplicates top stack element
  17. abac ..... + Add - pops top two elements and pushes their sum
  18. abba ..... [ L-brace - skip to matching ] if top stack element is 0
  19. abbb ..... o Output - pops and outputs top stack element
  20. abbc ..... * Multiply - pops top two elements and pushes their product
  21. abca ..... e Execute - Pops four elements and interprets them as an instruction
  22. abcb ..... - Negate - pops value from stack, pushes -(value)
  23. abcc ..... ! Pop - pops and discards top stack element
  24. abcd ..... ] R-brace - skip back to matching [
  25. """
  26.  
  27. def chunks(l, n):
  28. return (l[i:i+n] for i in range(0, len(l), n))
  29.  
  30. def convert(inst):
  31. i = 0
  32. for c in inst:
  33. i = i*4 + inst.index(c)
  34.  
  35. return "n..i....>.\\1....<d.+[o.*e-!]"[i]
  36.  
  37. def _compile(code):
  38. return map(convert, chunks(code, 4))
  39.  
  40. def main():
  41. stack = deque()
  42. code = input()
  43. code = _compile(code)
  44. #print("".join(code))
  45.  
  46. # program counter / instruction pointer
  47. pc = 0
  48.  
  49. while pc < len(code):
  50.  
  51. inst = code[pc]
  52.  
  53. if inst == 'n':
  54. pass
  55.  
  56. elif inst == 'i':
  57. stack.append(ord(input()))
  58.  
  59. elif inst == '>':
  60. stack.rotate(1)
  61.  
  62. elif inst == '\\':
  63. a = stack.pop()
  64. b = stack.pop()
  65. stack.append(a)
  66. stack.append(b)
  67.  
  68. elif inst == '1':
  69. stack.append(1)
  70.  
  71. elif inst == '<':
  72. stack.rotate(-1)
  73.  
  74. elif inst == 'd':
  75. a = stack.pop()
  76. stack.append(a)
  77. stack.append(a)
  78.  
  79. elif inst == '+':
  80. a = stack.pop()
  81. b = stack.pop()
  82. stack.append(a + b)
  83.  
  84. elif inst == '*':
  85. a = stack.pop()
  86. b = stack.pop()
  87. stack.append(a * b)
  88.  
  89. elif inst == 'o':
  90. a = stack.pop()
  91. print(chr(a))
  92.  
  93. elif inst == '-':
  94. a = stack.pop()
  95. stack.append(-a)
  96.  
  97. elif inst == '!':
  98. a = stack.pop()
  99.  
  100. elif inst == 'e':
  101. a = stack.pop()
  102. b = stack.pop()
  103. c = stack.pop()
  104. d = stack.pop()
  105. inst = convert([chr(a),chr(b),chr(c),chr(d)])
  106. continue
  107.  
  108. elif inst == '[':
  109. a = stack.pop()
  110. if a == 0:
  111. # advance to end of loop
  112. loop_level = 1
  113. while loop_level > 0:
  114. pc += 1
  115. if pc > len(code):
  116. raise Exception("Bracket mismatch")
  117. if code[pc] == '[': loop_level += 1
  118. elif code[pc] == ']': loop_level -= 1
  119.  
  120. elif inst == ']':
  121. # rewind to the start of the loop
  122. loop_level = 1
  123. while loop_level > 0:
  124. pc -= 1
  125. if pc < 0:
  126. raise Exception("Bracket mismatch")
  127. if code[pc] == '[': loop_level -= 1
  128. elif code[pc] == ']': loop_level += 1
  129. pc -= 1
  130.  
  131. else:
  132. raise Exception("Unknown instruction '%s' at index %s.".format(inst, pc))
  133.  
  134. pc += 1
  135.  
  136. main()
  137.  
  138.  
Success #stdin #stdout 0.01s 9136KB
stdin
22212111
#
stdout
#