Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. from itertools import permutations
  2.  
  3. def getval(param, mem, index, jump):
  4.     return mem[mem[index+jump]] if not param else mem[index+jump]
  5.  
  6. def computer(memory, inputs):
  7.     mem = memory[0]
  8.     ind = memory[1]
  9.     outputs = list()
  10.    
  11.     while True:
  12.         instruction = (str(mem[ind])).zfill(5)
  13.         param3 = int(instruction[0])
  14.         param2 = int(instruction[1])
  15.         param1 = int(instruction[2])
  16.         opcode = int(instruction[3:])
  17.        
  18.         if opcode == 1:
  19.             mem[mem[ind+3]] = getval(param1, mem, ind, 1) + getval(param2, mem, ind, 2)
  20.             ind += 4
  21.         elif opcode == 2:
  22.             mem[mem[ind+3]] = getval(param1, mem, ind, 1) * getval(param2, mem, ind, 2)
  23.             ind += 4
  24.         elif opcode == 3:
  25.             if len(inputs):
  26.                 mem[mem[ind + 1]] = inputs.pop(0)
  27.                 ind += 2
  28.             else:
  29.                 memory[1] = ind
  30.                 return outputs
  31.         elif opcode == 4:
  32.             outputs.append(getval(param1, mem, ind, 1))
  33.             ind += 2
  34.         elif opcode == 5:
  35.             ind = getval(param2, mem, ind, 2) if getval(param1, mem, ind, 1) else (ind + 3)
  36.         elif opcode == 6:
  37.             ind = getval(param2, mem, ind, 2) if not getval(param1, mem, ind, 1) else (ind + 3)
  38.         elif opcode == 7:
  39.             mem[mem[ind + 3]] = 1 if getval(param1, mem, ind, 1) < getval(param2, mem, ind, 2) else 0
  40.             ind += 4
  41.         elif opcode == 8:
  42.             mem[mem[ind + 3]] = 1 if getval(param1, mem, ind, 1) == getval(param2, mem, ind, 2) else 0
  43.             ind += 4
  44.         elif opcode == 99:
  45.             outputs.append('halt')
  46.             return outputs
  47.  
  48. with open('inputs.txt') as f:
  49.     memory = list(map(int, f.readline().split(',')))
  50.    
  51. signal = 0
  52.  
  53. for phases in list(permutations ([i for i in range(5,10)])):  
  54.  
  55.     mems = list()
  56.     for _ in range(5):
  57.         mems.append([memory[:],0])
  58.  
  59.     outputs = None
  60.     i = 0
  61.  
  62.     for phase in phases:
  63.         if not outputs :
  64.             inputs = [phase,0]
  65.         else:
  66.             inputs = [phase] + outputs
  67.         outputs = computer(mems[i], inputs)
  68.         i = (i+1) % 5
  69.  
  70.     while 'halt' not in outputs:
  71.         inputs = outputs
  72.         outputs = computer(mems[i], inputs)
  73.         i = (i+1) % 5
  74.    
  75.     outputs.pop(outputs.index('halt'))
  76.     for i in range(1,5):
  77.         inputs = outputs
  78.         outputs = computer(mems[i], inputs)
  79.         if 'halt' in outputs:
  80.             outputs.pop(outputs.index('halt'))
  81.            
  82.     if 'halt' in outputs:
  83.         outputs.pop(outputs.index('halt'))
  84.     signal = max(signal, max(outputs))
  85.  
  86. print(signal)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement