def compute(paint_start_point=False): # Run the program with open("input", "r") as hand: optcode_raw = hand.read() optcode = list(map(lambda el: int(el), optcode_raw.split(","))) interpreter = OptcodeInterpreter(optcode, quiet_mode=True) interpreter.run_async() start_point = (0, 0) robot = HullRobot(start_point) table = [] if paint_start_point: table.append(start_point) painted_points = [] while True: pos = robot.get_position() color = 1 if pos in table else 0 interpreter.input.put(color) try: output_color = interpreter.output.get(timeout=1) except Exception: break turn = interpreter.output.get() if output_color == 1 and not is_duplicate(pos, table): table.append(pos) elif output_color == 0 and is_duplicate(pos, table): table.remove(pos) if not is_duplicate(pos, painted_points): painted_points.append(pos) if turn == 0: robot.rotate_left() else: robot.rotate_right() robot.move() # print("Debug", len(table)) if paint_start_point: delta, size = get_image_shape(table) image = np.zeros(shape=size, dtype=np.uint8) dy, dx = delta for y, x in table: image[y + dy, x + dx] = 255 dec_image = Image.fromarray(image) dec_image.save("res.png") print("Saved code to image res.png") else: print("Painted points: %d" % len(painted_points))
def compute(): optcode = read_input() interpreter = OptcodeInterpreter(optcode, quiet_mode=True) thread = interpreter.run_async() arcade_screen = ArcadeScreen() while thread.is_alive(): draw_instructions(interpreter, arcade_screen) while not interpreter.output.empty(): draw_instructions(interpreter, arcade_screen) screen = arcade_screen.get_screen() return len(list(filter(lambda pos: screen[pos] == TILE_BLOCK, screen)))