Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. def getval(param, mem, index, jump, base):
  2.     if param == 0:
  3.         return mem[mem[index+jump]]
  4.     elif param == 1:
  5.         return mem[index+jump]
  6.     elif param == 2:
  7.         return mem[mem[index+jump]+base]
  8.  
  9. def getdest(param, mem, index, jump, base):
  10.     if param == 0:
  11.         dest = mem[index+jump]
  12.     elif param == 1:
  13.         dest = index + jump
  14.     elif param == 2:
  15.         dest = mem[index+jump] + base
  16.     return dest
  17.  
  18. def computer(memory, inputs):
  19.     mem = memory[0]
  20.     mem +=  [0]*max(mem)
  21.     ind = memory[1]
  22.     base = memory[2]
  23.     outputs = list()
  24.    
  25.     while True:
  26.         instruction = (str(mem[ind])).zfill(5)
  27.         param3 = int(instruction[0])
  28.         param2 = int(instruction[1])
  29.         param1 = int(instruction[2])
  30.         opcode = int(instruction[3:])
  31.         if opcode == 1:
  32.             mem[getdest(param3, mem, ind, 3, base)] = getval(param1, mem, ind, 1, base) + getval(param2, mem, ind, 2, base)
  33.             ind += 4
  34.         elif opcode == 2:
  35.             mem[getdest(param3, mem, ind, 3, base)] = getval(param1, mem, ind, 1, base) * getval(param2, mem, ind, 2, base)
  36.             ind += 4
  37.         elif opcode == 3:
  38.             if len(inputs):
  39.                 mem[getdest(param1, mem, ind, 1, base)] = inputs.pop(0)
  40.                 ind += 2
  41.             else:
  42.                 memory[1] = ind
  43.                 return outputs
  44.         elif opcode == 4:
  45.             outputs.append(getval(param1, mem, ind, 1, base))
  46.             ind += 2
  47.         elif opcode == 5:
  48.             ind = getval(param2, mem, ind, 2, base) if getval(param1, mem, ind, 1, base) else (ind + 3)
  49.         elif opcode == 6:
  50.             ind = getval(param2, mem, ind, 2, base) if not getval(param1, mem, ind, 1, base) else (ind + 3)
  51.         elif opcode == 7:
  52.             mem[getdest(param3, mem, ind, 3, base)] = 1 if getval(param1, mem, ind, 1, base) < getval(param2, mem, ind, 2, base) else 0
  53.             ind += 4
  54.         elif opcode == 8:
  55.             mem[getdest(param3, mem, ind, 3, base)] = 1 if getval(param1, mem, ind, 1, base) == getval(param2, mem, ind, 2, base) else 0
  56.             ind += 4
  57.         elif opcode == 9:
  58.             base += getval(param1, mem, ind, 1, base)
  59.             ind += 2
  60.         elif opcode == 99:
  61.             outputs.append('halt')
  62.             return outputs
  63.         else:
  64.             print("invalid opcode: {0}".format(opcode))
  65.             break
  66.            
  67. with open('inputs.txt') as f:
  68.     memory = list(map(int, f.readline().split(',')))
  69.    
  70. print(computer([memory, 0, 0], [2]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement