Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def intcode(startingValue, relativeBase, panelColor, x):
- outputArray = []
- inputInstruction = panelColor #send 0 or 1 to input instruction
- i = startingValue
- while i < len(x):
- op = int(x[i]%100)
- modes = int(x[i]/100)
- parameter1 = modes%10
- parameter2 = (modes//10)%10
- parameter3 = (modes//100)
- if (op == 1): #add
- #check len of x[i]
- #now do the operation
- first = x[i+1]
- second = x[i+2]
- if (parameter1 == 0):
- first = x[x[i+1]]
- if (parameter1 == 2):
- first = x[x[i+1]+relativeBase]
- if (parameter2 == 0):
- second = x[x[i+2]]
- if (parameter2 == 2):
- second = x[x[i+2]+relativeBase]
- result = first + second
- if (parameter3 == 0):
- x[x[i+3]] = result
- if (parameter3 == 1):
- x[i+3] = result
- if (parameter3 == 2):
- x[x[i+3] + relativeBase] = result
- i+=4
- #multiply
- if (op == 2):
- #check len of x[i]
- first = x[i+1]
- second = x[i+2]
- if (parameter1 == 0):
- first = x[x[i+1]]
- if (parameter1 == 2):
- first = x[x[i+1]+relativeBase]
- if (parameter2 == 0):
- second = x[x[i+2]]
- if (parameter2 == 2):
- second = x[x[i+2]+relativeBase]
- result = first * second
- if (parameter3 == 0):
- x[x[i+3]] = result
- if (parameter3 == 1):
- x[i+3] = result
- if (parameter3 == 2):
- x[x[i+3] + relativeBase] = result
- x[x[i+3]] = result
- i+=4
- #read
- if (op == 3):
- #asks for input
- if (parameter1 == 1):
- x[i+1] = inputInstruction
- if (parameter1 == 2):
- x[x[i+1] + relativeBase] = inputInstruction
- if (parameter1 == 0):
- x[x[i+1]] = inputInstruction
- i+=2
- #output
- if (op == 4):
- #now do the operation
- output = x[i+1]
- if (parameter1 == 2):
- output = x[x[i+1] + relativeBase]
- if (parameter1 == 0):
- output = x[x[i+1]]
- outputArray.append(output)
- #return output array after 2 outputs have occurred
- #send back the current state of intcode and the location to start at?
- if (len(outputArray) == 2):
- return (outputArray + [i+2] + [relativeBase], x)
- i+=2
- if (op == 5):
- #check len of x[i]
- # immediate mode / position mode
- first = x[i+1]
- second = x[i+2]
- if (parameter1 == 0):
- first = x[x[i+1]]
- if (parameter1 == 2):
- first = x[x[i+1]+relativeBase]
- if (parameter2 == 0):
- second = x[x[i+2]]
- if (parameter2 == 2):
- second = x[x[i+2]+relativeBase]
- if (first != 0 ):
- i = second
- else:
- i+=3
- if (op == 6):
- #check len of x[i]
- # immediate mode / position mode
- first = x[i+1]
- second = x[i+2]
- if (parameter1 == 0):
- first = x[x[i+1]]
- if (parameter1 == 2):
- first = x[x[i+1]+relativeBase]
- if (parameter2 == 0):
- second = x[x[i+2]]
- if (parameter2 == 2):
- second = x[x[i+2]+relativeBase]
- if (first == 0 ):
- i = second
- else:
- i+=3
- if (op == 7):
- #check len of x[i]
- # immediate mode / position mode
- first = x[i+1]
- second = x[i+2]
- if (parameter1 == 0):
- first = x[x[i+1]]
- if (parameter1 == 2):
- first = x[x[i+1]+relativeBase]
- if (parameter2 == 0):
- second = x[x[i+2]]
- if (parameter2 == 2):
- second = x[x[i+2]+relativeBase]
- if (first < second ):
- if (parameter3 == 0):
- x[x[i+3]] = 1
- if (parameter3 == 1):
- x[i+3] = 1
- if (parameter3 == 2):
- x[x[i+3] + relativeBase] = 1
- else:
- if (parameter3 == 0):
- x[x[i+3]] = 0
- if (parameter3 == 1):
- x[i+3] = 0
- if (parameter3 == 2):
- x[x[i+3] + relativeBase] = 0
- i+=4
- if (op == 8):
- # check len of x[i]
- # immediate mode / position mode
- first = x[i+1]
- second = x[i+2]
- if (parameter1 == 0):
- first = x[x[i+1]]
- if (parameter1 == 2):
- first = x[x[i+1]+relativeBase]
- if (parameter2 == 0):
- second = x[x[i+2]]
- if (parameter2 == 2):
- second = x[x[i+2]+relativeBase]
- if (first == second):
- if (parameter3 == 0):
- x[x[i+3]] = 1
- if (parameter3 == 1):
- x[i+3] = 1
- if (parameter3 == 2):
- x[x[i+3] + relativeBase] = 1
- else:
- if (parameter3 == 0):
- x[x[i+3]] = 0
- if (parameter3 == 1):
- x[i+3] = 0
- if (parameter3 == 2):
- x[x[i+3] + relativeBase] = 0
- i+=4
- if (op == 9):
- # check len of x[i]
- # immediate mode / position mode
- first = x[i+1]
- if (parameter1 == 0):
- first = x[x[i+1]]
- if (parameter1 == 2):
- first = x[x[i+1]+relativeBase]
- relativeBase += first
- i+=2
- if (op == 99):
- return ([999999,999999,999999], x)
- def advent11():
- f = open('C:\\Users\\Lucas\\Desktop\\advent\\adventday11.txt', 'r')
- inputToIntcode = f.readline().split(',')
- f.close()
- for i in range(len(inputToIntcode)):
- inputToIntcode[i] = int(inputToIntcode[i])
- inputToIntcode.extend([0] * 1000)
- robotPanelColor = 0
- intCodePointer = 0
- panelsColoredCount = 0
- boolTest = True
- relativeBase = 0
- w, h = 1000, 1000;
- matrix1 = [[(0,0) for x in range(w)] for y in range(h)]
- x = 500
- y = 500
- direction = '^'
- while (boolTest):
- firstArg, inputToIntcode = intcode(intCodePointer, relativeBase, robotPanelColor, inputToIntcode)
- intCodePointer = firstArg[2]
- relativeBase = firstArg[3]
- colorToPaintPanel = firstArg[0]
- directionToMoveRobot = int(firstArg[1])
- print (directionToMoveRobot)
- if (directionToMoveRobot == 0):
- #move robot left
- if (direction == '^'):
- direction = '<'
- matrix1[x][y] = colorToPaintPanel
- x = x - 1
- elif (direction == '<'):
- direction = 'v'
- matrix1[x][y] = colorToPaintPanel
- y = y - 1
- elif (direction == 'v'):
- direction = '>'
- matrix1[x][y] = colorToPaintPanel
- x = x + 1
- elif (direction == '>'):
- direction = '^'
- matrix1[x][y] = colorToPaintPanel
- y = y + 1
- if (directionToMoveRobot == 1):
- #move robot right
- if (direction == '^'):
- direction = '>'
- matrix1[x][y] = colorToPaintPanel
- x = x + 1
- elif (direction == '>'):
- direction = 'v'
- matrix1[x][y] = colorToPaintPanel
- y = y - 1
- elif (direction == 'v'):
- direction = '<'
- matrix1[x][y] = colorToPaintPanel
- x = x - 1
- elif (direction == '<'):
- direction = '^'
- matrix1[x][y] = colorToPaintPanel
- y = y + 1
- robotPanelColor = matrix1[x][y]
- if (firstArg[0] == 999999):
- #stop
- boolTest = False
- # move robot then pass in new coords
- print(advent11())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement