Advertisement
Musical_Muze

Day 5, Part 2

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