Advertisement
Guest User

Untitled

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