Advertisement
Musical_Muze

intcode_class

Dec 17th, 2019
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.81 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.     #create an object for Day 7
  10.     def __init__(self, name, file1, phase, pointer):
  11.         self.name = name
  12.         self.phase = phase
  13.         self.pointer = pointer
  14.         self.inputCount = 0
  15.         input1 = open(file1, "r")
  16.         self.intcode = input1.read()
  17.         input1.close()
  18.         self.intcode = self.intcode.split(",")
  19.         for i in range(len(self.intcode)):
  20.             self.intcode[i] = int(self.intcode[i])
  21.  
  22.     #create an object for Day 9
  23.     def __init__(self, name, file1):
  24.         self.name = name
  25.         input1 = open(file1, "r")
  26.         self.intcode = input1.read()
  27.         input1.close()
  28.         self.intcode = self.intcode.split(",")
  29.         for i in range(len(self.intcode)):
  30.             self.intcode[i] = int(self.intcode[i])
  31.         for i in range(10000):
  32.             self.intcode.append(0)
  33.    
  34.     def getCode(self):
  35.         return self.intcode[self.pointer]
  36.  
  37.     def setMemory(self,array1):
  38.         self.intcode = array1
  39.  
  40.     def setPointer(self, pointer):
  41.         self.pointer = pointer
  42.  
  43.     #method for Day 7, Part 2 -------------------------------------------------
  44.     def ampRun(self, inputValue):
  45.  
  46.         code = self.intcode
  47.         pos = self.pointer
  48.         #print(self.name + " starting at position " + str(pos))
  49.  
  50.         #function to return a five-element array to represent the intcode
  51.         def intcodeArray(int1):
  52.             intcode = list(str(int1))
  53.             if(len(intcode)<5):
  54.                 for i in range(5-len(intcode)):
  55.                     intcode.insert(0,"0")
  56.             for i in range(len(intcode)):
  57.                 intcode[i] = int(intcode[i])
  58.             return intcode
  59.  
  60.         ##inst[4] and inst[3] are the opcode
  61.         ##inst[2] is the mode of the first parameter
  62.         ##inst[1] is the mode of the second parameter
  63.         ##inst[0] is the mode of the third parameter
  64.  
  65.         #function to return the value of a position, given its mode
  66.         def value(mode, pos1):
  67.             val = 0
  68.             if(mode==0):
  69.                 val = code[code[pos1]]
  70.             else:
  71.                 val = code[pos1]
  72.             return val
  73.  
  74.         if(code[pos] == 99):
  75.             #print(self.name + " stopped at " + str(pos) + ", with code " + str(code[pos]))
  76.             returnValue = None
  77.  
  78.         #run the intcode
  79.         while(code[pos]!=99):
  80.            
  81.             inst = intcodeArray(code[pos])
  82.  
  83.             #add two values
  84.             if(inst[4]==1):
  85.                 two = pos+1
  86.                 three = pos+2
  87.                 four = code[pos+3]
  88.                
  89.                 code[four] = value(inst[2],two) + value(inst[1],three)
  90.                 pos += 4
  91.  
  92.             #multiply two values
  93.             elif(inst[4]==2):    
  94.                 two = pos+1
  95.                 three = pos+2
  96.                 four = code[pos+3]
  97.  
  98.                 code[four] = value(inst[2],two) * value(inst[1],three)
  99.                 pos += 4
  100.  
  101.             #take an input value
  102.             elif(inst[4]==3):
  103.                
  104.                 if(self.inputCount == 0):
  105.                     choice = self.phase
  106.                     self.inputCount += 1
  107.                 else:
  108.                     choice = inputValue
  109.                
  110.                 if(inst[2]==0):
  111.                     code[code[pos+1]] = choice
  112.                 else:
  113.                     code[pos+1] = choice
  114.                    
  115.                 pos += 2
  116.  
  117.             #return an output value
  118.             elif(inst[4]==4):
  119.                 returnValue = value(inst[2],(pos+1))
  120.                 #print(self.name + " stopped at " + str(pos) + ", with code " + str(code[pos]))
  121.                 pos += 2
  122.                 self.pointer = pos
  123.                 break
  124.  
  125.             #if first parameter != 0, jump to position given by the second parameter
  126.             elif(inst[4]==5):
  127.                
  128.                 two = pos+1
  129.                 three = pos+2
  130.                
  131.                 if(value(inst[2],two)!=0):
  132.                     pos = value(inst[1],three)
  133.                 else:
  134.                     pos += 3
  135.  
  136.             #if first parameter is zero, jump to position given by the second parameter
  137.             elif(inst[4]==6):
  138.                
  139.                 two = pos+1
  140.                 three = pos+2
  141.                
  142.                 if(value(inst[2],two)==0):
  143.                     pos = value(inst[1],three)
  144.                 else:
  145.                     pos += 3
  146.  
  147.             #based on whether first parameter < second parameter,
  148.             #store 1 or 0 in the position given by the third parameter
  149.             elif(inst[4]==7):
  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.             #based on whether first parameter == second parameter,
  162.             #store 1 or 1 in the position given by the third parameter.
  163.             elif(inst[4]==8):
  164.                
  165.                 two = pos+1
  166.                 three = pos+2
  167.                 four = code[pos+3]
  168.                
  169.                 if(value(inst[2],two) == value(inst[1],three)):
  170.                     code[four] = 1
  171.                 else:
  172.                     code[four] = 0
  173.                 pos += 4
  174.  
  175.         return returnValue
  176.    
  177.     #method for Day 9 ---------------------------------------------------------
  178.     def intcodeRun(self, inputValue):
  179.  
  180.         code = self.intcode
  181.  
  182.         pos = 0
  183.        
  184.         relPos = 0      #set the relative position
  185.  
  186.         #function to return a five-element array to represent the intcode
  187.         def intcodeArray(int1):
  188.             intcode = list(str(int1))
  189.             if(len(intcode)<5):
  190.                 for i in range(5-len(intcode)):
  191.                     intcode.insert(0,"0")
  192.             for i in range(len(intcode)):
  193.                 intcode[i] = int(intcode[i])
  194.             return intcode
  195.  
  196.         ##inst[4] and inst[3] are the opcode
  197.         ##inst[2] is the mode of the first parameter
  198.         ##inst[1] is the mode of the second parameter
  199.         ##inst[0] is the mode of the third parameter
  200.  
  201.         #function to return the value of a position, given its mode
  202.         def value(mode, pos1):
  203.             val = 0
  204.             if(mode==0):        #position mode
  205.                 val = code[code[pos1]]
  206.             elif(mode==1):      #immediate mode
  207.                 val = code[pos1]
  208.             elif(mode==2):      #relative mode
  209.                 val = code[code[pos1]+relPos]
  210.             return val
  211.  
  212.         #run the intcode
  213.         while(code[pos]!=99):
  214.            
  215.             inst = intcodeArray(code[pos])
  216.  
  217.             #add two values
  218.             if(inst[4]==1):
  219.                 two = pos+1
  220.                 three = pos+2
  221.                 four = pos+3
  222.  
  223.                 if(inst[0]==0):
  224.                     heather = code[four]
  225.                 elif(inst[0]==1):
  226.                     heather = four
  227.                 elif(inst[0]==2):
  228.                     heather = code[four] + relPos
  229.  
  230.                 code[heather] = value(inst[2],two) + value(inst[1],three)
  231.                 pos += 4
  232.  
  233.             #multiply two values
  234.             elif(inst[4]==2):    
  235.                 two = pos+1
  236.                 three = pos+2
  237.                 four = pos+3
  238.  
  239.                 if(inst[0]==0):
  240.                     heather = code[four]
  241.                 elif(inst[0]==1):
  242.                     heather = four
  243.                 elif(inst[0]==2):
  244.                     heather = code[four] + relPos
  245.  
  246.                 code[heather] = value(inst[2],two) * value(inst[1],three)
  247.                 pos += 4
  248.  
  249.             #take an input value
  250.             elif(inst[4]==3):
  251.                
  252.                 if(inst[2]==0):
  253.                     code[code[pos+1]] = inputValue
  254.                 elif(inst[2]==1):
  255.                     code[pos+1] = inputValue
  256.                 elif(inst[2]==2):
  257.                     code[code[pos+1]+relPos] = inputValue
  258.                    
  259.                 pos += 2
  260.  
  261.             #return an output value
  262.             elif(inst[4]==4):
  263.                 print("Returned " + str(value(inst[2],(pos+1))))
  264.                 pos += 2
  265.  
  266.             #if first parameter != 0, jump to position given by the second parameter
  267.             elif(inst[4]==5):
  268.                
  269.                 two = pos+1
  270.                 three = pos+2
  271.  
  272.                 if(value(inst[2],two)!=0):
  273.                     pos = value(inst[1],three)
  274.                 else:
  275.                     pos += 3
  276.  
  277.             #if first parameter is zero, jump to position given by the second parameter
  278.             elif(inst[4]==6):
  279.                
  280.                 two = pos+1
  281.                 three = pos+2
  282.  
  283.                 if(value(inst[2],two)==0):
  284.                     pos = value(inst[1],three)
  285.                 else:
  286.                     pos += 3
  287.  
  288.             #based on whether first parameter < second parameter,
  289.             #store 1 or 0 in the position given by the third parameter
  290.             elif(inst[4]==7):
  291.                
  292.                 two = pos+1
  293.                 three = pos+2
  294.                 #four = code[pos+3]
  295.                 four = pos+3
  296.  
  297.                 if(inst[0]==0):
  298.                     heather = code[four]
  299.                 elif(inst[0]==1):
  300.                     heather = four
  301.                 elif(inst[0]==2):
  302.                     heather = code[four] + relPos                
  303.  
  304.                 if(value(inst[2],two) < value(inst[1],three)):
  305.                     code[heather] = 1
  306.                 else:
  307.                     code[heather] = 0
  308.                 pos += 4
  309.  
  310.             #based on whether first parameter == second parameter,
  311.             #store 1 or 0 in the position given by the third parameter.
  312.             elif(inst[4]==8):
  313.                
  314.                 two = pos+1
  315.                 three = pos+2
  316.                 #four = code[pos+3]
  317.                 four = pos+3
  318.  
  319.                 if(inst[0]==0):
  320.                     heather = code[four]
  321.                 elif(inst[0]==1):
  322.                     heather = four
  323.                 elif(inst[0]==2):
  324.                     heather = code[four] + relPos                
  325.  
  326.                 if(value(inst[2],two) == value(inst[1],three)):
  327.                     code[heather] = 1
  328.                 else:
  329.                     code[heather] = 0
  330.                 pos += 4
  331.  
  332.             #change the relative position by the only parameter
  333.             elif(inst[4]==9):
  334.                 relPos += value(inst[2],(pos+1))
  335.  
  336.                 pos += 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement