Advertisement
Guest User

Adventcode day 2-2

a guest
Dec 4th, 2019
899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | None | 0 0
  1. filename = 'puzzleinput.txt'
  2.  
  3. with open(filename, 'r') as f:
  4.     intcode = f.read().rstrip()
  5.  
  6. original_intcode = [int(num) for num in intcode.split(',')]
  7. intcode = original_intcode[:]
  8.  
  9. def intcode_translator(intcode):
  10.     """Translate intcode.
  11.  
  12.    Intcode is a list of numbers, each 4 numbers x.y.z.d represents;
  13.    x = operation, 1 for +, 2 for *.
  14.    y = the position of the first input.
  15.    z = the position of the second input.
  16.    d = the position to save the result of the operation.
  17.    """
  18.  
  19.     opcode = intcode[0::4]
  20.     first_num = intcode[1::4]
  21.     second_num = intcode[2::4]
  22.     save_position = intcode[3::4]
  23.    
  24.     index = 0
  25.    
  26.     while True:
  27.         for num in opcode:
  28.             if num == 1:
  29.                 intcode[save_position[index]] = ( intcode[first_num[index]]
  30.                 + intcode[second_num[index]] )
  31.                 index += 1
  32.             elif num == 2:
  33.                 intcode[save_position[index]] = ( intcode[first_num[index]]
  34.                 * intcode[second_num[index]] )
  35.                 index += 1
  36.             elif num == 99:
  37.                 break
  38.         break
  39.  
  40. count = 0
  41.  
  42. while True:
  43.     for i in range(99):
  44.         intcode[1] = i
  45.         intcode_translator(intcode)
  46.         if intcode[0] == 19690720:
  47.             break
  48.         else:
  49.             intcode = original_intcode[:]
  50.             intcode[2] = count
  51.            
  52.     if intcode[0] == 19690720:
  53.         break
  54.     else:
  55.         count += 1
  56.         intcode[2] = count
  57.        
  58. print('\nThe translated incode list:')
  59. print(intcode)
  60. print('\nThe original list:')
  61. print(original_intcode)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement