Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import permutations
- def read_intcode():
- f = open('input', 'r')
- x = f.readlines()
- f.close()
- a_intcode = x[0].split(',')
- for i in xrange(0, len(a_intcode)):
- a_intcode[i] = int(a_intcode[i])
- return a_intcode
- def doAddition(i, intcode, first_value_type, second_value_type):
- if first_value_type == 0:
- first_input = intcode[intcode[i+1]]
- else:
- first_input = intcode[i+1]
- if second_value_type == 0:
- second_input = intcode[intcode[i+2]]
- else:
- second_input = intcode[i+2]
- intcode[intcode[i+3]] = first_input + second_input
- def doMultiplication(i, intcode, first_value_type, second_value_type):
- if first_value_type == 0:
- first_input = intcode[intcode[i+1]]
- else:
- first_input = intcode[i+1]
- if second_value_type == 0:
- second_input = intcode[intcode[i+2]]
- else:
- second_input = intcode[i+2]
- intcode[intcode[i+3]] = first_input * second_input
- def takeInput(i, intcode, inputVal):
- whatToInput = inputVal
- intcode[intcode[i+1]] = whatToInput
- def giveOutput(i, intcode, value_type):
- if (value_type == 0):
- return intcode[intcode[i+1]]
- else:
- return intcode[i+1]
- def jumpIfTrue(i, intcode, first_value_type, second_value_type):
- if (first_value_type == 0):
- isZero = intcode[intcode[i+1]]
- else:
- isZero = intcode[i+1]
- if (isZero != 0):
- if (second_value_type == 0):
- return intcode[intcode[i+2]]
- else:
- return intcode[i+2]
- return -1
- def jumpIfFalse(i, intcode, first_value_type, second_value_type):
- if (first_value_type == 0):
- isZero = intcode[intcode[i+1]]
- else:
- isZero = intcode[i+1]
- if (isZero == 0):
- if (second_value_type == 0):
- return intcode[intcode[i+2]]
- else:
- return intcode[i+2]
- return -1
- def lessThan(i, intcode, first_value_type, second_value_type):
- if (first_value_type == 0):
- first_input = intcode[intcode[i+1]]
- else:
- first_input = intcode[i+1]
- if (second_value_type == 0):
- second_input = intcode[intcode[i+2]]
- else:
- second_input = intcode[i+2]
- if (first_input < second_input):
- intcode[intcode[i+3]] = 1
- else:
- intcode[intcode[i+3]] = 0
- def doesEqual(i, intcode, first_value_type, second_value_type):
- if (first_value_type == 0):
- first_input = intcode[intcode[i+1]]
- else:
- first_input = intcode[i+1]
- if (second_value_type == 0):
- second_input = intcode[intcode[i+2]]
- else:
- second_input = intcode[i+2]
- if (first_input == second_input):
- intcode[intcode[i+3]] = 1
- else:
- intcode[intcode[i+3]] = 0
- def calculate(ampsDetails):
- notHalted = True
- i = ampsDetails[1]
- intcode = ampsDetails[0]
- inputList = ampsDetails[2]
- outputList = []
- while (i < len(intcode)):
- if intcode[i] == 99:
- notHalted = False
- break
- operation = intcode[i]
- if (operation == 1):
- doAddition(i, intcode, 0, 0)
- i = i + 4
- continue
- elif (operation == 2):
- doMultiplication(i, intcode, 0, 0)
- i = i + 4
- continue
- elif (operation == 3):
- if len(inputList) == 0:
- break
- takeInput(i, intcode, inputList[0])
- del inputList[0]
- i = i + 2
- continue
- elif (operation == 4):
- outputVal = giveOutput(i, intcode, 0)
- outputList.append(outputVal)
- i = i + 2
- continue
- elif (operation == 5):
- howMuchToJump = jumpIfTrue(i, intcode, 0, 0)
- if (howMuchToJump == -1):
- i = i + 3
- else:
- i = howMuchToJump
- continue
- elif (operation == 6):
- howMuchToJump = jumpIfFalse(i, intcode, 0, 0)
- if (howMuchToJump == -1):
- i = i + 3
- else:
- i = howMuchToJump
- continue
- elif (operation == 7):
- lessThan(i, intcode, 0, 0)
- i = i + 4
- continue
- elif (operation == 8):
- doesEqual(i, intcode, 0, 0)
- i = i + 4
- continue
- else:
- a = operation % 100
- if (a == 2):
- first_value_type = operation / 100 % 10
- second_value_type = operation / 1000 % 10
- doMultiplication(i, intcode, first_value_type, second_value_type)
- i = i + 4
- elif (a == 1):
- first_value_type = operation / 100 % 10
- second_value_type = operation / 1000 % 10
- doAddition(i, intcode, first_value_type, second_value_type)
- i = i + 4
- elif (a == 3):
- if len(inputList) == 0:
- break
- takeInput(i, intcode, inputList[0])
- del inputList[0]
- i = i + 2
- elif (a == 4):
- value_type = operation / 100
- outputVal = giveOutput(i, intcode, value_type)
- outputList.append(outputVal)
- i = i + 2
- elif (a == 5):
- first_value_type = operation / 100 % 10
- second_value_type = operation / 1000 % 10
- howMuchToJump = jumpIfTrue(i, intcode, first_value_type, second_value_type)
- if (howMuchToJump == -1):
- i = i + 3
- else:
- i = howMuchToJump
- continue
- elif (a == 6):
- first_value_type = operation / 100 % 10
- second_value_type = operation / 1000 % 10
- howMuchToJump = jumpIfFalse(i, intcode, first_value_type, second_value_type)
- if (howMuchToJump == -1):
- i = i + 3
- else:
- i = howMuchToJump
- continue
- elif (a == 7):
- first_value_type = operation / 100 % 10
- second_value_type = operation / 1000 % 10
- lessThan(i, intcode, first_value_type, second_value_type)
- i = i + 4
- continue
- elif (a == 8):
- first_value_type = operation / 100 % 10
- second_value_type = operation / 1000 % 10
- doesEqual(i, intcode, first_value_type, second_value_type)
- i = i + 4
- else:
- notHalted = False
- break
- result = []
- result.append(intcode)
- result.append(i)
- result.append(inputList)
- result.append(outputList)
- print i
- return result, notHalted
- def initializeAmps():
- 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]
- ampsList = ['A', 'B', 'C', 'D', 'E']
- ampsDetails = dict()
- ampsDetails['A'] = [intcode, 0, [0], []]
- ampsDetails['B'] = [intcode, 0, [], []]
- ampsDetails['C'] = [intcode, 0, [], []]
- ampsDetails['D'] = [intcode, 0, [], []]
- ampsDetails['E'] = [intcode, 0, [], []]
- return ampsList, ampsDetails
- phasePermutations = permutations([5, 6, 7, 8, 9], 5)
- maxPerm = []
- maxT = 0
- for phasePerm in phasePermutations:
- ampsList, ampsDetails = initializeAmps()
- print "initialized"
- for i in range(5):
- ampsDetails[ampsList[i]][2].insert(0, phasePerm[i])
- notHalted = True
- j = 0
- while notHalted:
- actualJ = j % 5
- print ampsList[(actualJ)%5]
- print ampsDetails.get(ampsList[actualJ])
- ampsDetails[ampsList[actualJ]], notHalted = calculate(ampsDetails.get(ampsList[actualJ]))
- if len(ampsDetails[ampsList[(actualJ+1)%5]][2]) > 0:
- justForTheFirstIteration = ampsDetails.get(ampsList[(actualJ+1)%5])[2][0]
- ampsDetails[ampsList[(actualJ+1)%5]][2] = ampsDetails[ampsList[actualJ]][3]
- ampsDetails[ampsList[(actualJ+1)%5]][2].insert(0, justForTheFirstIteration)
- else:
- ampsDetails[ampsList[(actualJ+1)%5]][2] = ampsDetails[ampsList[actualJ]][3]
- print "end of while:"
- print ampsDetails.get(ampsList[actualJ])
- j += 1
- print notHalted
- if ampsDetails.get('E')[3][0] > maxT:
- maxT = ampsDetails.get('E')[3][0]
- maxPerm = phasePerm
- print maxT
- print maxPerm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement