Advertisement
Musical_Muze

ampIntcode.py

Dec 12th, 2019
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.72 KB | None | 0 0
  1. def intcode(file1, phase, inputValue):
  2.  
  3.     inputCount = 0
  4.    
  5.     #extract the code from the file and load it into an array for processing
  6.     input1 = open(file1, "r")
  7.     code = input1.read()
  8.     input1.close()
  9.     code = code.split(",")
  10.     for i in range(len(code)):
  11.         code[i] = int(code[i])        
  12.  
  13.     #function to return a five-element array to represent the intcode
  14.     def intcodeArray(int1):
  15.         intcode = list(str(int1))
  16.         if(len(intcode)<5):
  17.             for i in range(5-len(intcode)):
  18.                 intcode.insert(0,"0")
  19.         for i in range(len(intcode)):
  20.             intcode[i] = int(intcode[i])
  21.         return intcode
  22.  
  23.     ##inst[4] and inst[3] are the opcode
  24.     ##inst[2] is the mode of the first parameter
  25.     ##inst[1] is the mode of the second parameter
  26.     ##inst[0] is the mode of the third parameter
  27.  
  28.     #function to return the value of a position, given its mode
  29.     def value(mode, position):
  30.         val = 0
  31.         if(mode==0):
  32.             val = code[code[position]]
  33.         else:
  34.             val = code[position]
  35.         return val
  36.  
  37.     pos = 0
  38.  
  39.     #run the intcode
  40.     while(code[pos]!=99):
  41.        
  42.         inst = intcodeArray(code[pos])
  43.  
  44.         #add two values
  45.         if(inst[4]==1):
  46.             two = pos+1
  47.             three = pos+2
  48.             four = code[pos+3]
  49.            
  50.             code[four] = value(inst[2],two) + value(inst[1],three)
  51.             pos += 4
  52.  
  53.         #multiply two values
  54.         elif(inst[4]==2):    
  55.             two = pos+1
  56.             three = pos+2
  57.             four = code[pos+3]
  58.  
  59.             code[four] = value(inst[2],two) * value(inst[1],three)
  60.             pos += 4
  61.  
  62.         #take an input value
  63.         elif(inst[4]==3):
  64.            
  65.             if(inputCount == 0):
  66.                 choice = phase
  67.                 inputCount += 1
  68.             else:
  69.                 choice = inputValue
  70.            
  71.             if(inst[2]==0):
  72.                 code[code[pos+1]] = choice
  73.             else:
  74.                 code[pos+1] = choice
  75.                
  76.             pos += 2
  77.  
  78.         #return an output value
  79.         elif(inst[4]==4):
  80.             returnValue = value(inst[2],(pos+1))
  81.             pos += 2
  82.  
  83.         #if first parameter != 0, jump to position given by the second parameter
  84.         elif(inst[4]==5):
  85.            
  86.             two = pos+1
  87.             three = pos+2
  88.            
  89.             if(value(inst[2],two)!=0):
  90.                 pos = value(inst[1],three)
  91.             else:
  92.                 pos += 3
  93.  
  94.         #if first parameter is zero, jump to position given by the second parameter
  95.         elif(inst[4]==6):
  96.            
  97.             two = pos+1
  98.             three = pos+2
  99.            
  100.             if(value(inst[2],two)==0):
  101.                 pos = value(inst[1],three)
  102.             else:
  103.                 pos += 3
  104.  
  105.         #based on whether first parameter < second parameter,
  106.         #store 1 or 0 in the position given by the third parameter
  107.         elif(inst[4]==7):
  108.            
  109.             two = pos+1
  110.             three = pos+2
  111.             four = code[pos+3]
  112.            
  113.             if(value(inst[2],two) < value(inst[1],three)):
  114.                 code[four] = 1
  115.             else:
  116.                 code[four] = 0
  117.             pos += 4
  118.  
  119.         #based on whether first parameter == second parameter,
  120.         #store 1 or 1 in the position given by the third parameter.
  121.         elif(inst[4]==8):
  122.            
  123.             two = pos+1
  124.             three = pos+2
  125.             four = code[pos+3]
  126.            
  127.             if(value(inst[2],two) == value(inst[1],three)):
  128.                 code[four] = 1
  129.             else:
  130.                 code[four] = 0
  131.             pos += 4
  132.  
  133.     return returnValue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement