def part2(intCode): gridDict = dict() grid = [] grid.append([]) targetPoint = (0, 0) for y in range(0, 10000): for x in range(0, 10000): if y == 1 or y == 2 or y == 4: # helps skip points that are not needed continue if y > (2 * x) and y >= 5: # helps skip points that are not needed grid[y].append(0) continue output, ptr, relativeBase = intCodeProgram.execute( copy.deepcopy(intCode), [x, y]) gridDict[(x, y)] = output[0] grid[y].append(output[0]) if grid[y][x] == 0 and grid[y][x - 1] == 1: break # if previous point was a beam, then skip the rest of points on x-axis if isFit(gridDict, x, y, 99): targetPoint = (x - 99, y - 99) print('point found: ', targetPoint) return targetPoint grid.append([]) #showMap(gridDict, targetPoint) return targetPoint
def createMap(intCode): output, ptr, relativeBase = intCodeProgram.execute(intCode, [0]) scaffoldDict = dict() x = y = 0 for i in output: if i == 35: scaffoldDict[(x,y)] = '#' x += 1 elif i == 46: scaffoldDict[(x,y)] = '.' x += 1 elif i == 74: scaffoldDict[(x,y)] = '<' x += 1 elif i == 76: scaffoldDict[(x,y)] = '<' x += 1 elif i == 94: scaffoldDict[(x,y)] = '^' x += 1 elif i == 166: scaffoldDict[(x,y)] = 'v' x += 1 elif i == 10: # newlines x = 0 y -= 1 return scaffoldDict
def execute(intCode): ptr = relativeBase = 0 inputs = [] while intCode[ptr] != 99: output, ptr, relativeBase = intCodeProgram.execute(intCode, inputs, ptr, relativeBase) if intCode[ptr] == 99 or intCode[ptr] == 3: break return output
def executeProgram(pathMap, intCode): main = list(map(ord, ['A', ',', 'B', ',', 'B', ',', 'C', ',', 'B', ',', 'C', ',', 'B', ',', 'C', ',', 'A', ',', 'A'])) main.append(10) A = list(map(ord, ['L', ',', '6', ',', 'R', ',', '8', ',', 'L', ',', '4', ',', 'R', ',', '8', ',', 'L', ',', '1', '2'])) A.append(10) B = list(map(ord, ['L', ',', '1', '2', ',', 'R', ',', '1', '0', ',', 'L', ',', '4'])) B.append(10) C = list(map(ord, ['L', ',', '1', '2', ',', 'L', ',', '6', ',', 'L', ',', '4', ',', 'L', ',', '4'])) C.append(10) continuousFeed = list(map(ord, ['n'])) continuousFeed.append(10) intCode[0] = 2 # start the robot print('starting the drone') output, ptr, relativeBase = intCodeProgram.execute(intCode, []) print('entering main routine:', main) output, ptr, relativeBase = intCodeProgram.execute(intCode, main, ptr, relativeBase) print('entering function A:', A) output, ptr, relativeBase = intCodeProgram.execute(intCode, A, ptr, relativeBase) print('entering function B:', B) output, ptr, relativeBase = intCodeProgram.execute(intCode, B, ptr, relativeBase) print('entering function C:', C) output, ptr, relativeBase = intCodeProgram.execute(intCode, C, ptr, relativeBase) print('entering continuous feed:', continuousFeed) output, ptr, relativeBase = intCodeProgram.execute(intCode, continuousFeed, ptr, relativeBase) print('final value:', output[len(output)-1]) return output[len(output)-1]
def part1(intCode): grid = [] for y in range(50): grid.append([]) for x in range(50): output, ptr, relativeBase = intCodeProgram.execute( copy.deepcopy(intCode), [x, y]) grid[y].append(output[0]) numPoints = len([(x, y) for y in range(len(grid)) for x in range(len(grid[0])) if grid[y][x] == 1]) return numPoints
def execute2(intCode): ptr = relativeBase = 0 inputs = [] gameBoardMap = dict() intCode[0] = 2 while intCode[ptr] != 99: output, ptr, relativeBase = intCodeProgram.execute(intCode, inputs, ptr, relativeBase) if intCode[ptr] == 99: break elif intCode[ptr] == 3: gameBoardMap = createGameBoard(output, gameBoardMap) xBall = list(gameBoardMap.keys())[list(gameBoardMap.values()).index(4)][0] xPaddle = list(gameBoardMap.keys())[list(gameBoardMap.values()).index(3)][0] #print('ball:', xBall, 'paddle:', xPaddle, 'Show score:', gameBoardMap[(-1,0)]) joystickVal = (xBall > xPaddle) - (xBall < xPaddle) inputs.append(joystickVal) return output
def execute(intCode, inputs): robotMap = dict() robotMap[(0, 0)] = getFloorColor(inputs[0]) initVal = inputs[0] xR, yR = (0, 0) ptr = relativeBase = dIndex = 0 while intCode[ptr] != 99: output, ptr, relativeBase = intCodeProgram.execute( intCode, inputs, ptr, relativeBase) if intCode[ptr] == 99: break robotMap[(xR, yR)] = getFloorColor(output[0]) xR, yR, dIndex = moveRobot(output[1], dIndex, xR, yR) if (xR, yR) not in robotMap: inputs.append(initVal) else: inputs.append(getFloorValue(robotMap[(xR, yR)])) return robotMap
def solve(intCode, mazeDict, wasHereDict, correctPathDict, directionIndex, x=0, y=0, ptr=0, relativeBase=0): outputVal, ptr, relativeBase = intCodeProgram.execute( intCode, [DIRECTIONS[directionIndex]], ptr, relativeBase) outputVal = outputVal[0] printMaze(mazeDict) if outputVal == MOVED_AND_GOAL: # if found print('found at %d,%d' % (x, y)) return True elif (x, y) in wasHereDict and wasHereDict[(x, y)] == '#': # if wall print('wall at %d,%d' % (x, y)) return False elif (x, y) in wasHereDict and wasHereDict[(x, y)] == '.': # if visited print('visited at %d,%d' % (x, y)) return False direction = DIRECTIONS[directionIndex] # get current direction if outputVal == WALL: if direction == NORTH: mazeDict[(x, y + 1)] = '#' elif direction == SOUTH: mazeDict[(x, y - 1)] = '#' elif direction == WEST: mazeDict[(x - 1, y)] = '#' elif direction == EAST: mazeDict[(x + 1, y)] = '#' directionIndex = directionIndex + 1 if directionIndex < 3 else 0 # change directions solve(intCode, mazeDict, wasHereDict, correctPathDict, directionIndex, x, y, ptr, relativeBase) if outputVal == MOVED: mazeDict[(x, y)] = '.' wasHereDict[(x, y)] = '.' # marked current position as visited if direction == NORTH: y += 1 elif direction == SOUTH: y -= 1 elif direction == WEST: x -= 1 elif direction == EAST: x += 1 mazeDict[(x, y)] = 'D' if solve(intCode, mazeDict, wasHereDict, correctPathDict, directionIndex, x, y, ptr, relativeBase): correctPathDict[(x, y)] = '.' return True directionIndex = directionIndex + 1 if directionIndex < 3 else 0 # change directions if solve(intCode, mazeDict, wasHereDict, correctPathDict, directionIndex, x, y, ptr, relativeBase): correctPathDict[(x, y)] = '.' return True directionIndex = directionIndex + 1 if directionIndex < 3 else 0 # change directions if solve(intCode, mazeDict, wasHereDict, correctPathDict, directionIndex, x, y, ptr, relativeBase): correctPathDict[(x, y)] = '.' return True directionIndex = directionIndex + 1 if directionIndex < 3 else 0 # change directions if solve(intCode, mazeDict, wasHereDict, correctPathDict, directionIndex, x, y, ptr, relativeBase): correctPathDict[(x, y)] = '.' return True # if solve(intCode, mazeDict, wasHereDict, correctPathDict, directionIndex, x, y, ptr, relativeBase): # correctPathDict[(x,y)] = '.' # return True #directionIndex = directionIndex + 1 if directionIndex < 3 else 0 return False