class Amplifier: name: str source = None target = None phase = None inputValue = None outputValue = None def __init__(self, name, inputs): self.name = name self.controller = IntCodeProcessor(inputs) def __repr__(self): return "<Amplifier %s [%s -> %s => %s]>" % ( self.name, self.source.name if self.source else ' ', self.name, self.target.name if self.target else ' ') def attach(self, amplifier): self.target = amplifier amplifier.source = self def setPhase(self, phase): self.phase = phase def reset(self): self.controller.reset() self.inputValue = None self.outputValue = None
def reset(self): self.facing = UP self.position = (50, 50) self.panels = [] self.controller = IntCodeProcessor(self.program[:]) self.controller.silent = True self.log = {}
def __init__(self, data): self.controller = IntCodeProcessor(data) self.screen = [] self.intersections = [] self.robot = None self.width = 0 self.height = 0 self.path = []
def __init__(self, source): self.source = source.copy() self.processor = IntCodeProcessor(self.source, self.getInput, self.sendOutput) self.screen = {} self.outCount = 0 self.outPos = [0, 0] self.screenSize = [0, 0]
def __init__(self, data): self.position = (0,0) self.controller = IntCodeProcessor(data) #self.controller.silent = False self.output = { (0,0): { "o": "*" }} self.direction = NORTH self.oxygen = None self.filled = {}
class RepairDroid: def __init__(self, source): self.source = source.copy() self.processor = IntCodeProcessor(self.source, self.getInput, self.sendOutput) self.pos = (50,0) self.map = defaultdict(int) self.xMax = 44 self.xMin = 0 self.yMax = 100 self.yMin = 0 self.direction = 1 self.steps = 0 self.oxLoc = None self.retHome = False self.inLoc = -1 self.sendX = True def getInput(self): retVal = self.pos[1] if self.sendX: retVal = self.pos[0] self.sendX = not self.sendX return retVal def sendOutput(self, out): self.map[self.pos] = out if out == 0 and self.pos[0] > self.xMin and self.pos[0] < self.xMax: self.xMin = self.pos[0] if out == 1 and self.pos[0] > self.xMax: self.xMax = self.pos[0] if self.pos[0] > self.xMax: self.pos = (self.xMin, self.pos[1] + 1) if self.pos[1] > self.yMax: self.yMax = self.pos[1] self.yMin = self.yMax - 100 else: self.pos = (self.pos[0] + 1, self.pos[1]) #self.printMap() def printMap(self): mapString = "----------------------MAP----------------------\n" for y in range(self.yMin, self.yMax): for x in range(self.xMin,self.xMax): mapString += str(self.map[(x,y)]) if (x,y) in self.map else "X" mapString += '\n' mapString += "X %d - %d Y %d - %d" % ( self.xMin, self.xMax, self.yMin, self.yMax) print (mapString) def run(self): self.processor.run() def reset(self, source = None): if source != None: self.processor.source = source.copy() self.processor.PC = 0
def __init__(self, data): self.controller = IntCodeProcessor(data) self.screen = {} self.credits = 0 self.playing = False self.ball = (0, 0) self.paddle = (0, 0) self.joystick = NEUTRAL self.run()
def __init__(self, source): self.source = source.copy() self.processor = IntCodeProcessor(self.source, self.getInput, self.sendOutput) self.screen = {} self.outCount = 0 self.outPos = [0,0] self.screenSize = [0,0] self.ballPos = [0,0] self.padPos = [0,0] self.score = 0 self.display = Tk() self.display.grid() self.buttonDict = {} self.blockSize = [10,10]
def __init__(self, source): self.source = source.copy() self.processor = IntCodeProcessor(self.source, self.getInput, self.sendOutput) self.pos = (0, 0) self.map = {(0, 0): START} self.xMax = 0 self.xMin = 0 self.yMax = 0 self.yMin = 0 self.direction = 1 self.steps = 0 self.oxLoc = None self.retHome = False
def __init__(self, source): self.source = source.copy() self.processor = IntCodeProcessor(self.source, self.getInput, self.sendOutput) self.pos = (50,0) self.map = defaultdict(int) self.xMax = 44 self.xMin = 0 self.yMax = 100 self.yMin = 0 self.direction = 1 self.steps = 0 self.oxLoc = None self.retHome = False self.inLoc = -1 self.sendX = True
class AftScaffoldingControlInformationInterface: def __init__(self, data): self.controller = IntCodeProcessor(data) self.screen = [] self.intersections = [] self.robot = None self.width = 0 self.height = 0 self.path = [] def start(self): line = [] while not self.controller.isFinished: output = self.controller.execute() if output == None: break if output == 10: output = ''.join(line) line = [] if output == "Command?": self.controller.inputValues = self.prompt() else: print(output) else: line.append(chr(output)) def prompt(self): command = input("Command? ") values = list(map(lambda char: ord(char), command)) values.append(10) return values
class ArcadeCabinet: def __init__(self, source): self.source = source.copy() self.processor = IntCodeProcessor(self.source, self.getInput, self.sendOutput) self.screen = {} self.outCount = 0 self.outPos = [0, 0] self.screenSize = [0, 0] def getInput(self): return 0 def sendOutput(self, out): if self.outCount == 2: self.screen[(self.outPos[0], self.outPos[1])] = out if self.outPos == self.screenSize: self.drawScreen() else: self.outPos[self.outCount] = out if out > self.screenSize[self.outCount]: self.screenSize[self.outCount] = out self.outCount = (self.outCount + 1) % 3 def run(self): self.processor.run() def drawScreen(self): print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") frame = [[" " for y in range(self.screenSize[1] + 1)] for x in range(self.screenSize[0] + 1)] for loc in self.screen: frame[loc[0]][loc[1]] = self.screen[loc] for y in range(self.screenSize[1] + 1): rowStr = "" for x in range(self.screenSize[0] + 1): rowStr += str(frame[x][y]) print(rowStr)
class Computer: def __init__(self, data): self.controller = IntCodeProcessor(data) #self.controller.silent = False def boot(self, address): self.address = address self.controller.inputValues = [address] print("Booting up computer on address", address) def run(self): print("RUN %d:" % self.address, packetQue[self.address]) inPacket = None outPacket = [] while not self.controller.isFinished: if len(self.controller.inputValues) == 0: if len(packetQue[self.address]) > 0: if inPacket != None: print("INPACK", inPacket) self.controller.inputValues.extend(inPacket) inPacket = None if len(self.controller.inputValues ) == 0 or not self.controller.inputValues[-1] == -1: print("EMRGNCY -1") self.controller.inputValues.append(-1) #self.controller.inputValues = [self.packetQue.pop(0)] outPacket.append(self.controller.execute()) if len(outPacket) == 3: print(self.address, "OUTPACK", outPacket) self.send(outPacket[0], outPacket[1:3]) outPacket = [] #input("%d >:" % self.address) break def send(self, recipient, packet=[]): print(self.address, "=> Send packet to", recipient, packet) packetQue[recipient].append(packet) print(recipient, "pq", packetQue[recipient])
class RepairDroid: def __init__(self, source): self.source = source.copy() self.processor = IntCodeProcessor(self.source, self.getInput, self.sendOutput) self.pos = (0, 0) self.map = {} self.xMax = 0 self.xMin = 0 self.yMax = 0 self.yMin = 0 self.direction = 1 self.steps = 0 self.oxLoc = None self.retHome = False self.inLoc = -1 def getInput(self): self.inLoc += 1 return COMMANDS[self.inLoc] def sendOutput(self, out): if out == 10: self.pos = (0, self.pos[1] + 1) self.yMax = self.pos[1] else: self.map[self.pos] = chr(out) self.pos = (self.pos[0] + 1, self.pos[1]) if self.pos[0] > self.xMax: self.xMax = self.pos[0] self.printMap() def printMap(self): mapString = "----------------------MAP----------------------\n" for y in range(self.yMin, self.yMax): for x in range(self.xMin, self.xMax): mapString += self.map[(x, y)] if (x, y) in self.map else "X" mapString += '\n' mapString += "X %d - %d Y %d - %d" % (self.xMin, self.xMax, self.yMin, self.yMax) print(mapString) def run(self): self.processor.run() def analyzeMap(self): alignment = 0 for loc in self.map: if self.map[loc] == '#': neighbors = [(loc[0] + 1, loc[1]), (loc[0] - 1, loc[1]), (loc[0], loc[1] + 1), (loc[0], loc[1] - 1)] intersection = True for neighbor in neighbors: intersection &= (neighbor in self.map and self.map[neighbor] == '#') if intersection: align = loc[0] * loc[1] print("ALIGNMENT for", loc, "=", align) alignment += align return alignment def getPathLength(self, start, move): steps = 0 loc = start nextStep = (loc[0] + move[0], loc[1] + move[1]) #print(nextStep) while nextStep in self.map and self.map[nextStep] == '#': loc = nextStep steps += 1 #print(loc,steps) nextStep = (loc[0] + move[0], loc[1] + move[1]) #print("PL",steps, loc) return steps, loc def getNextDirection(self, loc, move): if move == (0, -1): lMove = (-1, 0) rMove = (1, 0) elif move == (0, 1): lMove = (1, 0) rMove = (-1, 0) elif move == (1, 0): lMove = (0, -1) rMove = (0, 1) elif move == (-1, 0): lMove = (0, 1) rMove = (0, -1) lCell = (loc[0] + lMove[0], loc[1] + lMove[1]) rCell = (loc[0] + rMove[0], loc[1] + rMove[1]) lPath = False rPath = False if (lCell in self.map and self.map[lCell] == '#'): lPath = True if (rCell in self.map and self.map[rCell] == '#'): rPath = True if lPath and rPath: print("ERROR IN PATH Calculation") return "X", 0 elif lPath: return "L", lMove elif rPath: return "R", rMove else: return "X", 0 def getPath(self): for loc in self.map: if self.map[loc] == '^': curLoc = loc moveList = [] turn = 'L' move = (-1, 0) # Cheating and inputting defaults here while turn != 'X': moveList.append(turn) length, curLoc = self.getPathLength(curLoc, move) moveList.append(length) turn, move = self.getNextDirection(curLoc, move) return moveList
class ArcadeCabinet: def __init__(self, data): self.controller = IntCodeProcessor(data) self.screen = {} self.credits = 0 self.playing = False self.ball = (0, 0) self.paddle = (0, 0) self.joystick = NEUTRAL self.run() @property def isPlaying(self): return self.playing def run(self): while not self.controller.isFinished: x = self.controller.execute() y = self.controller.execute() id = self.controller.execute() if x == -1: self.score = id continue self.screen[(x, y)] = id if self.isPlaying: if id == PADDLE: self.paddle = (x, y) elif id == BALL: self.ball = (x, y) if x > self.paddle[0]: self.joystick = RIGHT elif x < self.paddle[0]: self.joystick = LEFT else: self.joystick = NEUTRAL self.controller.inputValues = [self.joystick] def insertQuarter(self): self.credits += 1 self.controller.inputs[0] += 1 def play(self): self.score = 0 self.credits -= 1 self.playing = True self.controller.reset() self.run() def render(self): for y in range(20): row = [] for x in range(50): if (x, y) in self.screen: row.append(SYMBOLS[self.screen[(x, y)]]) else: row.append(SYMBOLS[EMPTY]) print(''.join(row))
class AftScaffoldingControlInformationInterface: def __init__(self, data): self.controller = IntCodeProcessor(data) #self.controller.silent = False self.screen = [] self.intersections = [] self.robot = None self.width = 0 self.height = 0 self.path = [] def start(self): location = [0, 0] line = [] while not self.controller.isFinished: output = self.controller.execute() if output == None: self.render() break if output == 10: if len(line) > 0: self.screen.append(line) line = [] location = [0, location[1] + 1] else: if output == 94: self.robot = VacuumRobot(location, output) line.append(Pixel(location, chr(output))) location = [location[0] + 1, location[1]] if output not in [35, 46]: print(output, chr(output)) self.height, self.width = (len(self.screen), len(self.screen[0])) def render(self): self.intersections = [] for row in self.screen: line = [] for pixel in row: output = pixel.output if pixel.position[0] > 0 and pixel.position[1] > 0 and pixel.position[0] < self.width - 1 and pixel.position[1] < self.height - 1: above = self.screen[pixel.position[1] - 1][pixel.position[0]] below = self.screen[pixel.position[1] + 1][pixel.position[0]] left = self.screen[pixel.position[1]][pixel.position[0] - 1] right = self.screen[pixel.position[1]][pixel.position[0] + 1] intersection = ''.join([above.output, left.output, pixel.output, right.output, below.output]) if intersection == '#####': output = 'O' pixel.intersection = True self.intersections.append(pixel.position[0] * pixel.position[1]) line.append(output) print(''.join(line)) def analyze(self): position = self.robot.position heading = None bearing = -1 self.path = [] for n in range(10): if heading: position = [position[0]-1, position[1]] tile = self.getAt(position) print("xy:", position, tile) if tile.output == '#': self.path.append(heading) else: print("XXX") heading = None print("T", tile) if not heading: print("Find heading") adjacent = self.getAdjacentTo(position) for i in range(4): tile = adjacent[i] if tile and tile.output == '#': if bearing == -1: bearing = i #elif bearing == 3: # FInd out what is the direction to turn when going L or R heading = ['R', 'R', 'L', 'L'][i] break #self.path.append(heading) print("GOT", heading, bearing) #print("ADJ", adjacent, heading) print(self.path) def getAt(self, position): if position[0] < 0 or position[0] >= self.width or position[1] < 0 or position[1] >= self.height: return None return self.screen[position[1]][position[0]] def getAdjacentTo(self, position): x, y = position adjacent = [self.getAt((x, y-1)), self.getAt((x+1, y)), self.getAt((x, y+1)), self.getAt((x-1, y))] return adjacent
def __init__(self, name, inputs): self.name = name self.controller = IntCodeProcessor(inputs)
class Droid: @property def x(self): return self.position[0] @property def y(self): return self.position[1] @property def randomDirection(self): return ceil((random() * 4) % 4) def __init__(self, data): self.position = (0,0) self.controller = IntCodeProcessor(data) #self.controller.silent = False self.output = { (0,0): { "o": "*" }} self.direction = NORTH self.oxygen = None self.filled = {} def get(self, position): if position in self.output: return self.output[position] return None def explore(self): iterations = 0 visited = {} intersections = [] route = [self.position] while True: iterations += 1 visited[self.position] = True validMoves = [] for move in MOVES: position = self.position self.controller.inputValues = [move[0]] position = self.moved(move[0], position) status = self.controller.execute() representation = { "o": " ", "pos": position } if status == WALL: representation["o"] = " " representation[move[0]] = True self.output[position] = representation elif status == OXYGEN: self.oxygen = position representation["o"] = "O" representation[move[0]] = True self.output[position] = representation self.position = position self.render() print("FOUND OXYGEN at", position) break else: if position not in visited: validMoves.append(move[0]) representation["o"] = "." representation[move[0]] = True self.output[position] = representation self.controller.inputValues = [move[1]] self.controller.execute() if self.oxygen: print("Part 1:", len(route), self.oxygen) self.render(False) break if len(validMoves) == 1: self.direction = validMoves.pop() self.position = self.moved(self.direction, self.position) self.controller.inputValues = [self.direction] self.controller.execute() route.append(self.direction) elif len(validMoves) > 1: intersections.append(self.position) self.direction = validMoves.pop() self.position = self.moved(self.direction, self.position) self.controller.inputValues = [self.direction] self.controller.execute() route.append(self.direction) else: if len(intersections) > 0: while True: move = route.pop() if move == WEST: move = EAST elif move == EAST: move = WEST elif move == NORTH: move = SOUTH elif move == SOUTH: move = NORTH self.direction = move self.position = self.moved(self.direction, self.position) self.controller.inputValues = [self.direction] self.controller.execute() if self.position == intersections[-1]: intersections.pop() break else: print("NO MOVES", validMoves, intersections) break def fill(self): self.filled = {} print(self.output[self.oxygen]) size = 50 minutes = 0 while True: newCells = [] for y in range(size): for x in range(size): pos = (x - size // 2, y - size // 2) value = self.output[pos]["o"] if pos in self.output else " " if value not in ".OD": continue if pos in self.filled: west = (pos[0] - 1, pos[1]) east = (pos[0] + 1, pos[1]) north = (pos[0], pos[1] - 1) south = (pos[0], pos[1] + 1) for adjacent in (west, north, east, south): if not adjacent in self.output: continue if self.output[adjacent]["o"] == "." and adjacent not in self.filled: newCells.append(adjacent) for cell in newCells: self.filled[cell] = minutes print("MINUTES", minutes) self.render() input() minutes += 1 self.filled[self.oxygen] = 1 def moved(self, direction, origin): x = origin[0] + [0, 0, 0, -1, 1][direction] y = origin[1] + [0, -1, 1, 0, 0][direction] return (x, y) def render(self, showDroid=True): size = 50 for y in range(size): output = [] for x in range(size): pos = (x-size//2, y-size//2) value = self.output[pos]["o"] if pos in self.output else " " if pos == self.position and showDroid: value = "D" if pos in self.filled: value = "O" output.append(value) print(''.join(output))
it should only output a single value, the BOOST keycode. What BOOST keycode does it produce? ANSWER: 2932210790 --- Part Two --- You now have a complete Intcode computer. Finally, you can lock on to the Ceres distress signal! You just need to boost your sensors using the BOOST program. The program runs in sensor boost mode by providing the input instruction the value 2. Once run, it will boost the sensors automatically, but it might take a few seconds to complete the operation on slower hardware. In sensor boost mode, the program will output a single value: the coordinates of the distress signal. Run the BOOST program in sensor boost mode. What are the coordinates of the distress signal? ANSWER: 73144 """ from IntCodeProcessor import IntCodeProcessor data = open("data/9.data").read() parser = IntCodeProcessor(data) parser.silent = True parser.inputValues = [1] print("Part 1:", parser.execute()) parser.reset() parser.inputValues = [2] print("Part 2:", parser.execute())
if loc[1] < loc[5]: loc[5] = loc[1] if loc[0] < loc[3]: loc[3] = loc[0] mode[0] = 0 if __name__ == '__main__': with open('day11.txt') as inFile: sourceTxt = inFile.read().strip().split(',') source = {} for i in range(len(sourceTxt)): source[i] = int(sourceTxt[i]) procA = IntCodeProcessor( source, getInput=lambda: getColor(grid, loc), sendOutput=lambda x: paintAndDrive(grid, loc, mode, heading, x)) procA.run() #print(grid) print(len(grid)) outString = [[" " for x in range(loc[2] - loc[3] + 1)] for y in range(loc[4] - loc[5] + 1)] for key in grid: outString[(key[1] - loc[5])][(key[0] - loc[3])] = "█" if grid[key] == 0 else " " for row in outString:
class RepairDroid: def __init__(self, source): self.source = source.copy() self.processor = IntCodeProcessor(self.source, self.getInput, self.sendOutput) self.pos = (0, 0) self.map = {(0, 0): START} self.xMax = 0 self.xMin = 0 self.yMax = 0 self.yMin = 0 self.direction = 1 self.steps = 0 self.oxLoc = None self.retHome = False def mapDist(self, start): curCells = [start] i = 0 while len(curCells) > 0: toRemove = [] toAppend = [] for curCell in curCells: #print (curCell) if curCell in self.map and self.map[curCell] in [ SPACE, OXYGEN, START ]: toAppend.append((curCell[0] + 1, curCell[1])) toAppend.append((curCell[0] - 1, curCell[1])) toAppend.append((curCell[0], curCell[1] + 1)) toAppend.append((curCell[0], curCell[1] - 1)) self.map[curCell] = "%03d" % i toRemove.append(curCell) for cell in toRemove: curCells.remove(cell) for cell in toAppend: curCells.append(cell) i += 1 self.printMap() print("STEPS:", i) print("Oxygen system %s steps away." % self.map[self.oxLoc]) def getInput(self): #print("INPUT", self.direction) if self.retHome == False: return self.direction print("Returned Home") self.mapDist(self.oxLoc) def sendOutput(self, out): #print("OUTPUT", out) if self.direction == 1: targetCell = (self.pos[0], self.pos[1] + 1) elif self.direction == 2: targetCell = (self.pos[0], self.pos[1] - 1) elif self.direction == 3: targetCell = (self.pos[0] - 1, self.pos[1]) elif self.direction == 4: targetCell = (self.pos[0] + 1, self.pos[1]) if self.oxLoc != None and targetCell == (0, 0): self.retHome = True if targetCell not in self.map: self.steps += 1 #Track bounds of map if targetCell[0] > self.xMax: self.xMax = targetCell[0] elif targetCell[0] < self.xMin: self.xMin = targetCell[0] if targetCell[1] > self.yMax: self.yMax = targetCell[1] elif targetCell[1] < self.yMin: self.yMin = targetCell[1] #HIT A WALL, TURN LEFT if out == 0: if targetCell not in self.map: self.map[targetCell] = WALL if self.direction == 1: self.direction = 3 elif self.direction == 2: self.direction = 4 elif self.direction == 3: self.direction = 2 elif self.direction == 4: self.direction = 1 #SPACE AHEAD if out == 1: self.pos = targetCell if targetCell not in self.map: self.map[targetCell] = SPACE if self.direction == 1: self.direction = 4 elif self.direction == 2: self.direction = 3 elif self.direction == 3: self.direction = 1 elif self.direction == 4: self.direction = 2 if self.steps % 200 == 0: self.printMap() #OXYGEN TANK if out == 2: self.pos = targetCell self.map[targetCell] = OXYGEN self.oxLoc = targetCell self.printMap() print( "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO" ) print("oxLoc Found:", self.oxLoc) def printMap(self): mapString = "----------------------MAP----------------------\n" for y in range(self.yMin, self.yMax + 1): for x in range(self.xMin, self.xMax + 1): mapString += self.map[(x, y)] if (x, y) in self.map else "XXX" mapString += '\n' mapString += "X %d - %d Y %d - %d" % (self.xMin, self.xMax, self.yMin, self.yMax) print(mapString) def run(self): self.processor.run()
class EmergencyHullPaintingRobot(): @property def x(self): return self.position[0] @property def y(self): return self.position[1] def __init__(self, program): self.program = program def reset(self): self.facing = UP self.position = (50, 50) self.panels = [] self.controller = IntCodeProcessor(self.program[:]) self.controller.silent = True self.log = {} def determineStartingPosition(self): width = len(self.panels[0]) height = len(self.panels) self.position = (int(width / 2), int(height / 2)) for y, row in enumerate(self.panels): for x, col in enumerate(row): if self.panels[y][x] == "#": self.position = (x, y) return def start(self, panels): self.reset() self.panels = panels self.determineStartingPosition() while not self.controller.isFinished: self.controller.inputValues = [self.camera()] color = self.controller.execute() self.paint(color) direction = self.controller.execute() if self.controller.isFinished: break self.turn(direction) self.move() def render(self): output = panels[:] output[self.y][self.x] = DIRS[self.facing] for row in panels: print(''.join(row)) print() def turn(self, direction): self.facing = (self.facing + (-1, 1)[direction]) % 4 def move(self): x, y = self.position x += (0, 1, 0, -1)[self.facing] y += (-1, 0, 1, 0)[self.facing] self.position = (x, y) def camera(self): return 1 if self.panels[self.y][self.x] == "#" else 0 def paint(self, color): self.log[self.position] = True self.panels[self.y][self.x] = "#" if color == 1 else "."
Once the program has halted, its output is available at address 0, also just like before. Each time you try a pair of inputs, make sure you first reset the computer's memory to the values in the program (your puzzle input) - in other words, don't reuse memory from a previous attempt. Find the input noun and verb that cause the program to produce the output 19690720. What is 100 * noun + verb? (For example, if noun=12 and verb=2, the answer would be 1202.) Your puzzle answer was 7264. """ from IntCodeProcessor import IntCodeProcessor inputs = [ 1, 0, 0, 3, 1, 1, 2, 3, 1, 3, 4, 3, 1, 5, 0, 3, 2, 10, 1, 19, 1, 19, 9, 23, 1, 23, 6, 27, 1, 9, 27, 31, 1, 31, 10, 35, 2, 13, 35, 39, 1, 39, 10, 43, 1, 43, 9, 47, 1, 47, 13, 51, 1, 51, 13, 55, 2, 55, 6, 59, 1, 59, 5, 63, 2, 10, 63, 67, 1, 67, 9, 71, 1, 71, 13, 75, 1, 6, 75, 79, 1, 10, 79, 83, 2, 9, 83, 87, 1, 87, 5, 91, 2, 91, 9, 95, 1, 6, 95, 99, 1, 99, 5, 103, 2, 103, 10, 107, 1, 107, 6, 111, 2, 9, 111, 115, 2, 9, 115, 119, 2, 13, 119, 123, 1, 123, 9, 127, 1, 5, 127, 131, 1, 131, 2, 135, 1, 135, 6, 0, 99, 2, 0, 14, 0 ] parser = IntCodeProcessor(inputs) output = parser.execute(12, 2) print("Part 1:", parser.memory[0]) parser.reset() for noun in range(0, 100): for verb in range(0, 100): parser.reset() output = parser.execute(noun, verb) if parser.memory[0] == 19690720: print("Part 2:", noun, verb)
class ArcadeCabinet: def __init__(self, source): self.source = source.copy() self.processor = IntCodeProcessor(self.source, self.getInput, self.sendOutput) self.screen = {} self.outCount = 0 self.outPos = [0,0] self.screenSize = [0,0] self.ballPos = [0,0] self.padPos = [0,0] self.score = 0 self.display = Tk() self.display.grid() self.buttonDict = {} self.blockSize = [10,10] #self.display.mainloop() def getInput(self): retVal = 0 if self.ballPos[0] > self.padPos[0]: retVal = 1 if self.ballPos[0] < self.padPos[0]: retVal = -1 return retVal def sendOutput(self, out): if self.outCount == 2: if self.outPos[0] == -1 and self.outPos[1] == 0: self.score = out else: if out == 4: self.ballPos[0] = self.outPos[0] self.ballPos[1] = self.outPos[1] if out == 3: self.padPos[0] = self.outPos[0] self.padPos[1] = self.outPos[1] #self.drawScreen(out) #self.drawTkScreen(out) else: self.outPos[self.outCount] = out self.outCount = (self.outCount + 1) % 3 def run(self): self.processor.run() def drawTkScreen(self, out): #Create canvas object if (self.outPos[0],self.outPos[1]) not in self.buttonDict: self.buttonDict[(self.outPos[0],self.outPos[1])] = Button(self.display) self.buttonDict[(self.outPos[0],self.outPos[1])].grid(row=self.outPos[1], column = self.outPos[0]) self.buttonDict[(self.outPos[0],self.outPos[1])]["bg"] = COLORS[out] self.display.update() time.sleep(FRAMETIME) def drawScreen(self, out): if self.outPos[0] > self.screenSize[0]: self.screenSize[0] = self.outPos[0] if self.outPos[1] > self.screenSize[1]: self.screenSize[1] = self.outPos[1] self.screen[(self.outPos[0],self.outPos[1])] = out frame = [[" " for y in range(self.screenSize[1]+1)] for x in range(self.screenSize[0]+1)] for loc in self.screen: frame[loc[0]][loc[1]] = self.screen[loc] frameBuf = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" frameBuf += "~~~~~~~~~~~~~ SCORE: %010d ~~~~~~~~~~~~\n" % self.score frameBuf += "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" for y in range(self.screenSize[1]+1): rowStr = "" for x in range(self.screenSize[0]+1): rowStr += ICONS[frame[x][y]] if frame[x][y] in range(len(ICONS)) else ICONS[0] frameBuf += rowStr + '\n' os.system('cls') print (frameBuf)
Here's a larger example: 3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31, 1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104, 999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99 The above example program uses an input instruction to ask for a single number. The program will then output 999 if the input value is below 8, output 1000 if the input value is equal to 8, or output 1001 if the input value is greater than 8. This time, when the TEST diagnostic program runs its input instruction to get the ID of the system to test, provide it 5, the ID for the ship's thermal radiator controller. This diagnostic test suite only outputs one number, the diagnostic code. What is the diagnostic code for system ID 5? ANSWER: 14110739 """ inputs = [3,225,1,225,6,6,1100,1,238,225,104,0,1002,188,27,224,1001,224,-2241,224,4,224,102,8,223,223,1001,224,6,224,1,223,224,223,101,65,153,224,101,-108,224,224,4,224,1002,223,8,223,1001,224,1,224,1,224,223,223,1,158,191,224,101,-113,224,224,4,224,102,8,223,223,1001,224,7,224,1,223,224,223,1001,195,14,224,1001,224,-81,224,4,224,1002,223,8,223,101,3,224,224,1,224,223,223,1102,47,76,225,1102,35,69,224,101,-2415,224,224,4,224,102,8,223,223,101,2,224,224,1,224,223,223,1101,32,38,224,101,-70,224,224,4,224,102,8,223,223,101,3,224,224,1,224,223,223,1102,66,13,225,1102,43,84,225,1101,12,62,225,1102,30,35,225,2,149,101,224,101,-3102,224,224,4,224,102,8,223,223,101,4,224,224,1,223,224,223,1101,76,83,225,1102,51,51,225,1102,67,75,225,102,42,162,224,101,-1470,224,224,4,224,102,8,223,223,101,1,224,224,1,223,224,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,1108,226,677,224,1002,223,2,223,1005,224,329,101,1,223,223,108,226,226,224,1002,223,2,223,1005,224,344,1001,223,1,223,1107,677,226,224,1002,223,2,223,1006,224,359,101,1,223,223,1008,226,226,224,1002,223,2,223,1005,224,374,101,1,223,223,8,226,677,224,102,2,223,223,1006,224,389,101,1,223,223,7,226,677,224,1002,223,2,223,1005,224,404,1001,223,1,223,7,226,226,224,1002,223,2,223,1005,224,419,101,1,223,223,107,226,677,224,1002,223,2,223,1005,224,434,101,1,223,223,107,226,226,224,1002,223,2,223,1005,224,449,1001,223,1,223,1107,226,677,224,102,2,223,223,1006,224,464,1001,223,1,223,1007,677,226,224,1002,223,2,223,1006,224,479,1001,223,1,223,1107,677,677,224,1002,223,2,223,1005,224,494,101,1,223,223,1108,677,226,224,102,2,223,223,1006,224,509,101,1,223,223,7,677,226,224,1002,223,2,223,1005,224,524,1001,223,1,223,1008,677,226,224,102,2,223,223,1005,224,539,1001,223,1,223,1108,226,226,224,102,2,223,223,1005,224,554,101,1,223,223,107,677,677,224,102,2,223,223,1006,224,569,1001,223,1,223,1007,226,226,224,102,2,223,223,1006,224,584,101,1,223,223,8,677,677,224,102,2,223,223,1005,224,599,1001,223,1,223,108,677,677,224,1002,223,2,223,1005,224,614,101,1,223,223,108,226,677,224,102,2,223,223,1005,224,629,101,1,223,223,8,677,226,224,102,2,223,223,1006,224,644,1001,223,1,223,1007,677,677,224,1002,223,2,223,1006,224,659,1001,223,1,223,1008,677,677,224,1002,223,2,223,1005,224,674,101,1,223,223,4,223,99,226] parser = IntCodeProcessor(inputs) parser.silent = True parser.inputValues = [1] while not parser.isFinished: value = parser.execute() if value == 0: continue print("Part 1:", value) parser.reset() parser.inputValues = [5] while not parser.isFinished: value = parser.execute() if value == 0:
from IntCodeProcessor import IntCodeProcessor if __name__ == '__main__': with open('day9.txt') as inFile: sourceTxt = inFile.read().strip().split(',') source = {} for i in range(len(sourceTxt)): source[i] = int(sourceTxt[i]) procA = IntCodeProcessor(source) procA.run()
def __init__(self, data): self.controller = IntCodeProcessor(data)
class DroneSystem: def __init__(self, data): self.controller = IntCodeProcessor(data) self.scanMap = {} def scan(self, startingAt=(0, 0), width=10, height=10): self.startingAt = startingAt self.width = width self.height = height self.scanMap = {} for y in range(self.startingAt[1], self.startingAt[1] + self.height): line = [] beamLeft = -1 beamRight = -1 for x in range(self.startingAt[0], self.startingAt[0] + self.width): reading = self.examine((x, y)) self.scanMap[(x, y)] = reading if reading == 1: line.append('#') if beamLeft == -1: beamLeft = x beamRight = x else: line.append('\u00B7') print(''.join(line), y, [beamLeft, beamRight], beamRight - beamLeft) def examine(self, position): self.controller.resetMemory() self.controller.instructionPointer = 0 self.controller.relativeBase = 0 self.controller.inputValues = list(position) return self.controller.execute() def findQuadrant(self, size=5): y = 0 run = True while y < 5000 and run: x = 0 while x < 5000: if system.findBeamQuadrant((x, y), size): return (x, y) x += size y += size def findBeamQuadrant(self, position, size=5): scanResult = [] positions = [ position, (position[0] + size - 1, position[1]), (position[0] + size - 1, position[1] + size - 1), (position[0], position[1] + size - 1) ] for pos in positions: scanResult.append(self.examine(pos)) return sum(scanResult) == 4 def getScanMap(self, position): if position[0] < self.width - 1 and position[1] < self.height - 1: return (position, self.scanMap[position]) return (position, -1) def render(self): counter = 0 for y in range(self.startingAt[1], self.startingAt[1] + self.height): line = [] for x in range(self.startingAt[0], self.startingAt[0] + self.width): if self.scanMap[(x, y)] == 2: line.append('\u25CC') else: #if self.scanMap[(x, y)] == 1: # counter += 1 line.append('#' if self.scanMap[(x, y)] == 1 else '\u00B7') print(''.join(line), y)