def test_part1(self): i = IntcodeSim.fromFile("inputs/q05") i.queueInput(1).run() self.assertEqual(i.outputs[-1], 7692125, 'correct diagnostic code') self.assertTrue(all(x == 0 for x in i.outputs[1:-2]), 'all other outputs are 0')
def worker(code, inQueue, outQueue): """ amplifier worker function. accepts input from inQueue and sends output to outQueue """ sim = IntcodeSim(code) sim.inputFn = inQueue.get sim.outputFn = outQueue.put sim.run()
def part2(): screen = Screen() @dataclass class State(): args: List[int] = field(default_factory=list) score: int = 0 step: int = 0 ballX: Optional[int] = None batX: Optional[int] = None state = State() def handleOutput(output: int) -> None: if len(state.args) < 2: # Record x,y co-ordinates for next call state.args.append(output) return x, y = state.args state.args.clear() if x == -1 and y == 0: state.score = output else: tile = Tile(output) screen.set(x, y, tile) # Keep track of bat/ball position if tile == Tile.bat: state.batX = x elif tile == Tile.ball: state.ballX = x state.step += 1 # Wait for the machine to output the whole screen once # before we start rendering if state.step > 900: print(screen.render()) print("Score: " + str(state.score)) def handleInput() -> int: return util.sign(state.ballX - state.batX) i = IntcodeSim.fromFile("inputs/q13") # Set machine to 'play for free' mode i.setMemory(0, 2) i.inputFn = handleInput i.outputFn = handleOutput i.run() print(f"finished with score: {state.score}")
def part1(): screen = Screen() state = [] def handleOutput(output: int): nonlocal state if len(state) < 3: state.append(output) if len(state) == 3: x, y, tileNo = state state = [] screen.set(x, y, Tile(tileNo)) i = IntcodeSim.fromFile("inputs/q13") i.outputFn = handleOutput i.run() renderedScreen = screen.render() print(renderedScreen) blockTiles = screen.countTiles(Tile.block) print(blockTiles)
def test_part2(self): i = IntcodeSim.fromFile('inputs/q05') i.queueInput(5).run() self.assertEqual(i.outputs, [14340395])
""" def distanceToDeadEnd(point, cameFrom=None): "returns the distance from point to a dead-end" neighbours = self.neighboursOf(point) neighbours.discard(cameFrom) if neighbours: return 1 + max(distanceToDeadEnd(n, point) for n in neighbours) else: # We reached a dead-end: return 0 return distanceToDeadEnd(self.oxygen_system) handler = BotHandler() i = IntcodeSim.fromFile("inputs/q15") i.inputFn = lambda: handler.handleInput() i.outputFn = lambda status: handler.handleOutput(status) i.run() handler.paintMap() # Now work out distance from droid to oxygen handler, using A* droid_position = (0, 0) oxygen_position = handler.oxygen_system print(f"droid at {droid_position}, oxygen at {oxygen_position}") assert droid_position is not None assert oxygen_position is not None route = handler.findRoute(start=droid_position, goal=oxygen_position) handler.paintMap(route=route) print(route) # Route length should not include the droid
def test_part1(self): i = IntcodeSim.fromFile('inputs/q09') # Run in test mode using input 1 i.queueInput(1).run() i.run() self.assertEqual(i.outputs, [3345854957])
def test_part2(self): i = IntcodeSim.fromFile('inputs/q09') # Run in test mode using input 1 i.queueInput(2).run() i.run() self.assertEqual(i.outputs, [68938])
"each direction is 90' right of the previous" up = 0 right = 1 down = 2 left = 3 MOVEMENT = { Direction.up: (0, -1), Direction.right: (1, 0), Direction.left: (-1, 0), Direction.down: (0, 1), } code = slurp('inputs/q11') i = IntcodeSim(code) # Robot position pos = (0, 0) # Robot turning direction facing = Direction.up # Dict mapping co-ords to the color painted painted = {} # For part2, robot is starting on a white panel instead # Comment out for part1. painted[pos] = Color.white def handleInput(): "returns color of robot's current panel"