Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.97 KB | None | 0 0
  1. from itertools import permutations
  2.  
  3. def read_intcode():
  4.     f = open('input', 'r')
  5.     x = f.readlines()
  6.     f.close()
  7.     a_intcode = x[0].split(',')
  8.  
  9.     for i in xrange(0, len(a_intcode)):
  10.         a_intcode[i] = int(a_intcode[i])
  11.     return a_intcode
  12.  
  13. def doAddition(i, intcode, first_value_type, second_value_type):
  14.     if first_value_type == 0:
  15.         first_input = intcode[intcode[i+1]]
  16.     else:
  17.         first_input = intcode[i+1]
  18.     if second_value_type == 0:
  19.         second_input = intcode[intcode[i+2]]
  20.     else:
  21.         second_input = intcode[i+2]
  22.     intcode[intcode[i+3]] = first_input + second_input
  23.  
  24. def doMultiplication(i, intcode, first_value_type, second_value_type):
  25.     if first_value_type == 0:
  26.         first_input = intcode[intcode[i+1]]
  27.     else:
  28.         first_input = intcode[i+1]
  29.     if second_value_type == 0:
  30.         second_input = intcode[intcode[i+2]]
  31.     else:
  32.         second_input = intcode[i+2]
  33.     intcode[intcode[i+3]] = first_input * second_input
  34.  
  35. def takeInput(i, intcode, inputVal):
  36.     whatToInput = inputVal
  37.     intcode[intcode[i+1]] = whatToInput
  38.  
  39. def giveOutput(i, intcode, value_type):
  40.     if (value_type == 0):
  41.         return intcode[intcode[i+1]]
  42.     else:
  43.         return intcode[i+1]
  44.  
  45. def jumpIfTrue(i, intcode, first_value_type, second_value_type):
  46.     if (first_value_type == 0):
  47.         isZero = intcode[intcode[i+1]]
  48.     else:
  49.         isZero = intcode[i+1]
  50.     if (isZero != 0):
  51.         if (second_value_type == 0):
  52.             return intcode[intcode[i+2]]
  53.         else:
  54.             return intcode[i+2]
  55.     return -1
  56.  
  57. def jumpIfFalse(i, intcode, first_value_type, second_value_type):
  58.     if (first_value_type == 0):
  59.         isZero = intcode[intcode[i+1]]
  60.     else:
  61.         isZero = intcode[i+1]
  62.     if (isZero == 0):
  63.         if (second_value_type == 0):
  64.             return intcode[intcode[i+2]]
  65.         else:
  66.             return intcode[i+2]
  67.     return -1
  68.  
  69. def lessThan(i, intcode, first_value_type, second_value_type):
  70.     if (first_value_type == 0):
  71.         first_input = intcode[intcode[i+1]]
  72.     else:
  73.         first_input = intcode[i+1]
  74.     if (second_value_type == 0):
  75.         second_input = intcode[intcode[i+2]]
  76.     else:
  77.         second_input = intcode[i+2]
  78.     if (first_input < second_input):
  79.         intcode[intcode[i+3]] = 1
  80.     else:
  81.         intcode[intcode[i+3]] = 0
  82.  
  83. def doesEqual(i, intcode, first_value_type, second_value_type):
  84.     if (first_value_type == 0):
  85.         first_input = intcode[intcode[i+1]]
  86.     else:
  87.         first_input = intcode[i+1]
  88.     if (second_value_type == 0):
  89.         second_input = intcode[intcode[i+2]]
  90.     else:
  91.         second_input = intcode[i+2]
  92.     if (first_input == second_input):
  93.         intcode[intcode[i+3]] = 1
  94.     else:
  95.         intcode[intcode[i+3]] = 0
  96.  
  97. def calculate(ampsDetails):
  98.     notHalted = True
  99.     i = ampsDetails[1]
  100.     intcode = ampsDetails[0]
  101.     inputList = ampsDetails[2]
  102.     outputList = []
  103.  
  104.     while (i < len(intcode)):
  105.         if intcode[i] == 99:
  106.             notHalted = False
  107.             break
  108.         operation = intcode[i]
  109.  
  110.         if (operation == 1):
  111.             doAddition(i, intcode, 0, 0)
  112.             i = i + 4
  113.             continue
  114.         elif (operation == 2):
  115.             doMultiplication(i, intcode, 0, 0)
  116.             i = i + 4
  117.             continue
  118.         elif (operation == 3):
  119.             if len(inputList) == 0:
  120.                 break
  121.             takeInput(i, intcode, inputList[0])
  122.             del inputList[0]
  123.             i = i + 2
  124.             continue
  125.         elif (operation == 4):
  126.             outputVal = giveOutput(i, intcode, 0)
  127.             outputList.append(outputVal)
  128.             i = i + 2
  129.             continue
  130.         elif (operation == 5):
  131.             howMuchToJump = jumpIfTrue(i, intcode, 0, 0)
  132.             if (howMuchToJump == -1):
  133.                 i = i + 3
  134.             else:
  135.                 i = howMuchToJump
  136.             continue
  137.         elif (operation == 6):
  138.             howMuchToJump = jumpIfFalse(i, intcode, 0, 0)
  139.             if (howMuchToJump == -1):
  140.                 i = i + 3
  141.             else:
  142.                 i = howMuchToJump
  143.             continue
  144.         elif (operation == 7):
  145.             lessThan(i, intcode, 0, 0)
  146.             i = i + 4
  147.             continue
  148.         elif (operation == 8):
  149.             doesEqual(i, intcode, 0, 0)
  150.             i = i + 4
  151.             continue
  152.         else:
  153.             a = operation % 100
  154.             if (a == 2):
  155.                 first_value_type = operation / 100 % 10
  156.                 second_value_type = operation / 1000 % 10
  157.                 doMultiplication(i, intcode, first_value_type, second_value_type)
  158.                 i = i + 4
  159.             elif (a == 1):
  160.                 first_value_type = operation / 100 % 10
  161.                 second_value_type = operation / 1000 % 10
  162.                 doAddition(i, intcode, first_value_type, second_value_type)
  163.                 i = i + 4
  164.             elif (a == 3):
  165.                 if len(inputList) == 0:
  166.                     break
  167.                 takeInput(i, intcode, inputList[0])
  168.                 del inputList[0]
  169.                 i = i + 2
  170.             elif (a == 4):
  171.                 value_type = operation / 100
  172.                 outputVal = giveOutput(i, intcode, value_type)
  173.                 outputList.append(outputVal)
  174.                 i = i + 2
  175.             elif (a == 5):
  176.                 first_value_type = operation / 100 % 10
  177.                 second_value_type = operation / 1000 % 10
  178.                 howMuchToJump = jumpIfTrue(i, intcode, first_value_type, second_value_type)
  179.                 if (howMuchToJump == -1):
  180.                     i = i + 3
  181.                 else:
  182.                     i = howMuchToJump
  183.                 continue
  184.             elif (a == 6):
  185.                 first_value_type = operation / 100 % 10
  186.                 second_value_type = operation / 1000 % 10
  187.                 howMuchToJump = jumpIfFalse(i, intcode, first_value_type, second_value_type)
  188.                 if (howMuchToJump == -1):
  189.                     i = i + 3
  190.                 else:
  191.                     i = howMuchToJump
  192.                 continue
  193.             elif (a == 7):
  194.                 first_value_type = operation / 100 % 10
  195.                 second_value_type = operation / 1000 % 10
  196.                 lessThan(i, intcode, first_value_type, second_value_type)
  197.                 i = i + 4
  198.                 continue
  199.             elif (a == 8):
  200.                 first_value_type = operation / 100 % 10
  201.                 second_value_type = operation / 1000 % 10
  202.                 doesEqual(i, intcode, first_value_type, second_value_type)
  203.                 i = i + 4
  204.             else:
  205.                 notHalted = False
  206.                 break
  207.     result = []
  208.     result.append(intcode)
  209.     result.append(i)
  210.     result.append(inputList)
  211.     result.append(outputList)
  212.     print i
  213.     return result, notHalted
  214.  
  215. def initializeAmps():
  216.     intcode = [3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5]
  217.     ampsList = ['A', 'B', 'C', 'D', 'E']
  218.     ampsDetails = dict()
  219.     ampsDetails['A'] = [intcode, 0, [0], []]
  220.     ampsDetails['B'] = [intcode, 0, [], []]
  221.     ampsDetails['C'] = [intcode, 0, [], []]
  222.     ampsDetails['D'] = [intcode, 0, [], []]
  223.     ampsDetails['E'] = [intcode, 0, [], []]
  224.  
  225.     return ampsList, ampsDetails
  226.  
  227. phasePermutations = permutations([5, 6, 7, 8, 9], 5)
  228.  
  229. maxPerm = []
  230. maxT = 0
  231. for phasePerm in phasePermutations:
  232.     ampsList, ampsDetails = initializeAmps()
  233.     print "initialized"
  234.     for i in range(5):
  235.         ampsDetails[ampsList[i]][2].insert(0, phasePerm[i])
  236.     notHalted = True
  237.     j = 0
  238.     while notHalted:
  239.         actualJ = j % 5
  240.         print ampsList[(actualJ)%5]
  241.         print ampsDetails.get(ampsList[actualJ])
  242.         ampsDetails[ampsList[actualJ]], notHalted = calculate(ampsDetails.get(ampsList[actualJ]))
  243.         if len(ampsDetails[ampsList[(actualJ+1)%5]][2]) > 0:
  244.             justForTheFirstIteration = ampsDetails.get(ampsList[(actualJ+1)%5])[2][0]
  245.             ampsDetails[ampsList[(actualJ+1)%5]][2] = ampsDetails[ampsList[actualJ]][3]
  246.             ampsDetails[ampsList[(actualJ+1)%5]][2].insert(0, justForTheFirstIteration)
  247.         else:
  248.             ampsDetails[ampsList[(actualJ+1)%5]][2] = ampsDetails[ampsList[actualJ]][3]
  249.         print "end of while:"
  250.         print ampsDetails.get(ampsList[actualJ])
  251.         j += 1
  252.         print notHalted
  253.     if ampsDetails.get('E')[3][0] > maxT:
  254.         maxT = ampsDetails.get('E')[3][0]
  255.         maxPerm = phasePerm
  256. print maxT
  257. print maxPerm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement