Advertisement
Musical_Muze

intcode_class.py

Dec 12th, 2019
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.12 KB | None | 0 0
  1. class Intcode:
  2.  
  3.     #instance attributes:
  4.     #intcode - the intcode in memory
  5.     #phase - the phase of the engine
  6.     #pointer - a memory of where the intcode pointer instruction is/was
  7.     #inputCounter - controls the input flow of the machine
  8.  
  9.     def __init__(self, name, file1, phase, pointer):
  10.         self.name = name
  11.         self.phase = phase
  12.         self.pointer = pointer
  13.         self.inputCount = 0
  14.         input1 = open(file1, "r")
  15.         self.intcode = input1.read()
  16.         input1.close()
  17.         self.intcode = self.intcode.split(",")
  18.         for i in range(len(self.intcode)):
  19.             self.intcode[i] = int(self.intcode[i])
  20.    
  21.     def getCode(self):
  22.         return self.intcode[self.pointer]
  23.  
  24.     def setMemory(self,array1):
  25.         self.intcode = array1
  26.  
  27.     def setPointer(self, pointer):
  28.         self.pointer = pointer
  29.  
  30.     def ampRun(self, inputValue):
  31.  
  32.         code = self.intcode
  33.         pos = self.pointer
  34.         #print(self.name + " starting at position " + str(pos))
  35.  
  36.         #function to return a five-element array to represent the intcode
  37.         def intcodeArray(int1):
  38.             intcode = list(str(int1))
  39.             if(len(intcode)<5):
  40.                 for i in range(5-len(intcode)):
  41.                     intcode.insert(0,"0")
  42.             for i in range(len(intcode)):
  43.                 intcode[i] = int(intcode[i])
  44.             return intcode
  45.  
  46.         ##inst[4] and inst[3] are the opcode
  47.         ##inst[2] is the mode of the first parameter
  48.         ##inst[1] is the mode of the second parameter
  49.         ##inst[0] is the mode of the third parameter
  50.  
  51.         #function to return the value of a position, given its mode
  52.         def value(mode, pos1):
  53.             val = 0
  54.             if(mode==0):
  55.                 val = code[code[pos1]]
  56.             else:
  57.                 val = code[pos1]
  58.             return val
  59.  
  60.         if(code[pos] == 99):
  61.             #print(self.name + " stopped at " + str(pos) + ", with code " + str(code[pos]))
  62.             returnValue = None
  63.  
  64.         #run the intcode
  65.         while(code[pos]!=99):
  66.            
  67.             inst = intcodeArray(code[pos])
  68.  
  69.             #add two values
  70.             if(inst[4]==1):
  71.                 two = pos+1
  72.                 three = pos+2
  73.                 four = code[pos+3]
  74.                
  75.                 code[four] = value(inst[2],two) + value(inst[1],three)
  76.                 pos += 4
  77.  
  78.             #multiply two values
  79.             elif(inst[4]==2):    
  80.                 two = pos+1
  81.                 three = pos+2
  82.                 four = code[pos+3]
  83.  
  84.                 code[four] = value(inst[2],two) * value(inst[1],three)
  85.                 pos += 4
  86.  
  87.             #take an input value
  88.             elif(inst[4]==3):
  89.                
  90.                 if(self.inputCount == 0):
  91.                     choice = self.phase
  92.                     self.inputCount += 1
  93.                 else:
  94.                     choice = inputValue
  95.                
  96.                 if(inst[2]==0):
  97.                     code[code[pos+1]] = choice
  98.                 else:
  99.                     code[pos+1] = choice
  100.                    
  101.                 pos += 2
  102.  
  103.             #return an output value
  104.             elif(inst[4]==4):
  105.                 returnValue = value(inst[2],(pos+1))
  106.                 #print(self.name + " stopped at " + str(pos) + ", with code " + str(code[pos]))
  107.                 pos += 2
  108.                 self.pointer = pos
  109.                 break
  110.  
  111.             #if first parameter != 0, jump to position given by the second parameter
  112.             elif(inst[4]==5):
  113.                
  114.                 two = pos+1
  115.                 three = pos+2
  116.                
  117.                 if(value(inst[2],two)!=0):
  118.                     pos = value(inst[1],three)
  119.                 else:
  120.                     pos += 3
  121.  
  122.             #if first parameter is zero, jump to position given by the second parameter
  123.             elif(inst[4]==6):
  124.                
  125.                 two = pos+1
  126.                 three = pos+2
  127.                
  128.                 if(value(inst[2],two)==0):
  129.                     pos = value(inst[1],three)
  130.                 else:
  131.                     pos += 3
  132.  
  133.             #based on whether first parameter < second parameter,
  134.             #store 1 or 0 in the position given by the third parameter
  135.             elif(inst[4]==7):
  136.                
  137.                 two = pos+1
  138.                 three = pos+2
  139.                 four = code[pos+3]
  140.                
  141.                 if(value(inst[2],two) < value(inst[1],three)):
  142.                     code[four] = 1
  143.                 else:
  144.                     code[four] = 0
  145.                 pos += 4
  146.  
  147.             #based on whether first parameter == second parameter,
  148.             #store 1 or 1 in the position given by the third parameter.
  149.             elif(inst[4]==8):
  150.                
  151.                 two = pos+1
  152.                 three = pos+2
  153.                 four = code[pos+3]
  154.                
  155.                 if(value(inst[2],two) == value(inst[1],three)):
  156.                     code[four] = 1
  157.                 else:
  158.                     code[four] = 0
  159.                 pos += 4
  160.  
  161.         return returnValue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement