Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. # computer = [3,0,4,0,3,0,4,0,99]
  2. # computer = [1101,100,-1,4,0]
  3. computer = [data here]
  4. current_index = 0
  5. while(current_index < len(computer)):
  6.     if(computer[current_index] == 1):
  7.         computer[computer[current_index + 3]] = computer[computer[current_index + 1]] + computer[computer[current_index + 2]]
  8.         current_index += 4
  9.     elif(computer[current_index] == 2):
  10.         computer[computer[current_index + 3]] = computer[computer[current_index + 1]] * computer[computer[current_index + 2]]
  11.         current_index += 4
  12.     elif (computer[current_index] == 3):
  13.         computer[computer[current_index + 1]] = int(input("input: "))
  14.         current_index += 2
  15.     elif (computer[current_index] == 4):
  16.         print("output: ", computer[computer[current_index + 1]])
  17.         current_index += 2
  18.     elif (computer[current_index] == 99):
  19.         break
  20.     else:
  21.         # be careful of comparing chars with ints
  22.         opcode = str(computer[current_index])
  23.         # third parameter always in position mode
  24.         parameter_1_value = computer[current_index + 1] if opcode[1] == '1' else computer[computer[current_index + 1]]
  25.         parameter_2_value = computer[current_index + 2] if opcode[0] == '1' else computer[computer[current_index + 2]]
  26.  
  27.         output_index = computer[current_index + 3]
  28.  
  29.         op = int(opcode[len(opcode) - 1])
  30.         if(op == 1):
  31.             computer[output_index] = parameter_1_value + parameter_2_value
  32.             current_index += 4
  33.         elif(op == 2):
  34.             computer[output_index] = parameter_1_value * parameter_2_value
  35.             current_index += 4
  36.  
  37.     # print(computer[current_index]) # 104 not a command thats why its getting stuck but how?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement