def explore(program: Program) -> Grid: program.reset() available_moves: DefaultDict[complex, List[int]] = defaultdict(lambda: [4, 2, 3, 1]) backtracking: List[int] = list() grid: Grid = dict() position = 0j available_moves[position] # init to enter the loop while any(available_moves.values()) or backtracking: if available_moves[position]: direction = available_moves[position].pop() program.add_input(direction) status = next(program.operate()) grid[position + moves[direction]] = status if status in [1, 2]: backtracking.append(back[direction]) position += moves[direction] else: # We hit a wall, don't move continue else: # no move available, backtrack direction = backtracking.pop() program.add_input(direction) next(program.operate()) # we know it will be 1 position += moves[direction] return grid
def test_corners(program: Program, x: int, y: int) -> bool: for xx, yy in [(x, y), (x + 99, y), (x, y + 99), (x + 99, y + 99)]: program.reset() program.add_input(xx) program.add_input(yy) if next(program.operate()) != 1: return False return True
def scan(program: Program, shape: Tuple[int, int] = (50, 50)) -> np.array: grid = np.zeros(shape, dtype=int) for x in range(shape[0]): for y in range(shape[1]): program.add_input(x) program.add_input(y) grid[x][y] = next(program.operate()) program.reset() return grid
from intcode import Program if __name__ == "__main__": total = 0 for x in range(50): for y in range(50): bot = Program("input.txt") bot.add_input(x) bot.add_input(y) bot.run() total += bot.last_output print(total)
def test_point(row, col): bot = Program("input.txt") bot.add_input(row) bot.add_input(col) bot.run() return bot.last_output
from intcode import Program # solved on paper main = "A,B,A,B,C,A,B,C,A,C\n" A = "R,6,L,6,L,10\n" B = "L,8,L,6,L,10,L,6\n" C = "R,6,L,8,L,10,R,6\n" if __name__ == "__main__": camera = Program("input.txt") camera.program[0] = 2 [camera.add_input(ord(c)) for c in main] [camera.add_input(ord(c)) for c in A] [camera.add_input(ord(c)) for c in B] [camera.add_input(ord(c)) for c in C] # no camera output camera.add_input(ord("n")) camera.add_input(ord("\n")) camera.run() print(camera.get_outputs()[-1])