Advertisement
Musical_Muze

Day 2, Part 2

Dec 3rd, 2019
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. for x in range(100):
  2.     for y in range(100):
  3.         #extract the code from the file and load it into an array for processing
  4.         input = open("intcode.txt", "r")
  5.         code = input.read()
  6.         input.close()
  7.         code = code.split(",")
  8.  
  9.         #the array elements are currently strings; turn them into integers
  10.         for i in range(len(code)):
  11.             code[i] = int(code[i])
  12.  
  13.         #set the initial inputs
  14.         code[1] = x
  15.         code[2] = y
  16.  
  17.         #run the intcode
  18.         for i in range(0, len(code)-1, 4):
  19.             one = code[i]
  20.             two = code[i+1]
  21.             three = code[i+2]
  22.             four = code[i+3]
  23.             if (one == 1):
  24.                 code[four] = code[two] + code[three]
  25.             elif (one == 2):
  26.                 code[four] = code[two] * code[three]
  27.             elif (one == 99):
  28.                 break
  29.  
  30.         #check if the output matches the desired answer and print the result to the console.
  31.         if(code[0] == 19690720):
  32.             print("The inputs needed to obtain the output of 19690720 are " + str(x) + " and " + str(y))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement