Summary
Jump to navigation
Jump to search
Summary is a list of numbers where the sum of consecutive numbers determines the command
Interpreter
def interpret(nums):
stack = []
i = 0
while i < len(nums)-1:
sum_ = nums[i]+nums[i+1]
if sum_ == 1:
if i+2 < len(nums):
stack.append(nums[i+2])
i += 1
elif sum_ == 2:
if stack: stack.pop()
elif sum_ == 3:
if stack: print(stack.pop())
elif sum_ == 4:
if len(stack) > 1: stack.append(stack.pop()+stack.pop())
elif sum_ == 5:
if len(stack) > 1: stack.append(stack.pop()-stack.pop())
elif sum_ == 6:
if i+2 < len(nums) and -1<nums[i+2]<len(nums):
i = nums[i+2]-1
elif sum_ == 7:
if stack and stack.pop()==0 and i+2 < len(nums) and -1<nums[i+2]<len(nums):
i = nums[i+2]-1
elif sum_ == 8:
if stack:
stack.append(stack[-1])
elif sum_ == 9:
if stack and stack.pop()>0 and i+2 < len(nums) and -1<nums[i+2]<len(nums):
i = nums[i+2]-1
elif sum_ == 10:
if stack and stack.pop()<0 and i+2 < len(nums) and -1<nums[i+2]<len(nums):
i = nums[i+2]-1
i += 1
nums = [1,0,1,7,-4,5,1,3,5,-2,8,4] #count up program
interpret(nums)