Exemplo n.º 1
0
def get_bound(position, direction):
    x, y = position
    keep = True
    prev_point = None
    prev_status = None
    boundary = None
    while keep:
        optcode = read_input()
        interpreter = OptcodeInterpreter(optcode, quiet_mode=True)
        interpreter.input.put(x)
        interpreter.input.put(y)
        interpreter.run()
        point_status = interpreter.output.get()
        # Check if we changed status
        if prev_point is not None and point_status != prev_status:
            # Found boundary
            keep = False
            if point_status == 1:
                boundary = y
            else:
                boundary = prev_point
        else:
            # Move x towards outside
            if point_status == 0:
                delta = -1
            else:
                delta = 1
            prev_point = y
            prev_status = point_status
            y = y + delta * direction
    return boundary
Exemplo n.º 2
0
def alert_all_droids():
    space = get_space_configuration()
    moves = get_all_moves(space)
    moves, sequence = split_moves(moves)
    a, b, c = moves
    # Run
    optcode = read_input()
    optcode[0] = 2
    interpreter = OptcodeInterpreter(optcode, quiet_mode=True)
    # Input sequence
    sequence_str = convert_sequence(sequence)
    for el in sequence_str:
        interpreter.input.put(ord(el))
    # Configure routines
    for el in convert_sequence(a):
        interpreter.input.put(ord(el))
    for el in convert_sequence(b):
        interpreter.input.put(ord(el))
    for el in convert_sequence(c):
        interpreter.input.put(ord(el))
    interpreter.input.put(ord('n'))
    interpreter.input.put(ord('\n'))
    interpreter.run()
    last_val = None
    while not interpreter.output.empty():
        last_val = interpreter.output.get()
    return last_val
Exemplo n.º 3
0
def compute():
    # 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)
    interpreter.input.put(2)
    interpreter.run()
Exemplo n.º 4
0
def get_space_configuration():
    space = Space()
    camera = Camera(space)
    optcode = read_input()
    interpreter = OptcodeInterpreter(optcode, quiet_mode=True)
    interpreter.run()
    while not interpreter.output.empty():
        element = interpreter.output.get()
        camera.view(element)
    return space
Exemplo n.º 5
0
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)))
Exemplo n.º 6
0
def get_points_in_beam():
    counter = 0
    for x in range(50):
        for y in range(50):
            optcode = read_input()
            interpreter = OptcodeInterpreter(optcode, quiet_mode=True)
            interpreter.input.put(x)
            interpreter.input.put(y)
            interpreter.run()
            res = interpreter.output.get()
            counter += res
    return counter
Exemplo n.º 7
0
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))
Exemplo n.º 8
0
def compute_play(interactive=True):
    if interactive:
        print("Press A to move left, D to move right")
        print("Press ENTER to start")
        input()
    optcode = read_input()
    interpreter = OptcodeInterpreter(optcode, quiet_mode=True)
    interpreter.memory[0] = 2
    arcade_screen = ArcadeScreen()
    player = Player(arcade_screen)

    def on_input_requested():
        while not interpreter.output.empty():
            draw_instructions(interpreter, arcade_screen)
        if interactive:
            arcade_screen.render()
            int_input = get_input()
        else:
            int_input = player.act()
        interpreter.input.put(int_input)

    interpreter.set_input_request_listener(on_input_requested)
    interpreter.run()
    while not interpreter.output.empty():
        draw_instructions(interpreter, arcade_screen)
    if interactive:
        arcade_screen.render()
    return arcade_screen.get_score()
Exemplo n.º 9
0
def get_hull_damage(extended_mode=False):
    optcode = read_input()
    interpreter = OptcodeInterpreter(optcode, ascii_mode=True, quiet_mode=True)

    if not extended_mode:
        springscript = [
            "NOT A J", "NOT B T", "AND D T", "OR T J", "NOT C T", "AND D T",
            "OR T J", "WALK"
        ]
    else:
        springscript = [
            "NOT H T", "OR E T", "AND F T", "NOT E J", "AND J T", "OR C T",
            "NOT T J", "NOT C J", "AND C J", "OR B J", "OR E J", "AND T J",
            "AND A J", "NOT J J", "AND D J", "RUN"
        ]
    ascii_instructions = "\n".join(springscript) + "\n"
    for el in ascii_instructions:
        interpreter.input.put(el)

    interpreter.run()
    return interpreter.output.get()
Exemplo n.º 10
0
    def add_nic(self, optcode):
        address = len(self.network)
        instructions = [code for code in optcode]
        interpreter = OptcodeInterpreter(instructions, quiet_mode=True)

        def on_input_requested():
            if interpreter.input.empty():
                interpreter.input.put(-1)
                sleep(0.5)

        interpreter.set_input_request_listener(on_input_requested)

        def on_output_sent(value):
            packet = self._packets[address]
            if packet is None:
                self._packets[address] = [value, None, None]
            elif packet[1] is None:
                packet[1] = value
            else:
                packet[2] = value
                target, x, y = packet
                if target == 255:
                    if not self.monitor_idle:
                        self._run = False
                    self.nat_packet = [x, y]
                else:
                    self.network[target].input.put(x)
                    self.network[target].input.put(y)
                self._packets[address] = None

        interpreter.set_output_request_listener(on_output_sent)
        # Assign address
        interpreter.input.put(address)
        self.network.append(interpreter)
        self._packets.append(None)
Exemplo n.º 11
0
def explore():
    optcode = read_input()
    interpreter = OptcodeInterpreter(optcode, ascii_mode=True)

    def on_input_requested():
        if interpreter.input.empty():
            instructions = input("Input:")
            if instructions == "help":
                print("""

Possible commands are
- Movement via north, south, east, or west
- To take an item the droid sees in the environment, use the command take <name of item>. For example, if the droid reports seeing a red ball, you can pick it up with take red ball.
- To drop an item the droid is carrying, use the command drop <name of item>. For example, if the droid is carrying a green ball, you can drop it with drop green ball.
- To get a list of all of the items the droid is currently carrying, use the command inv (for "inventory").

Extra spaces or other characters aren't allowed - instructions must be provided precisely.


                """)
                on_input_requested()
            else:
                if not instructions.endswith("\n"):
                    instructions += "\n"
                    for el in instructions:
                        interpreter.input.put(el)

    interpreter.set_input_request_listener(on_input_requested)
    interpreter.run()
Exemplo n.º 12
0
def compute():
    space = SpaceMap()
    droid = RepairDroid()
    space.explore(droid.get_position(), POINT_EMPTY)
    optcode = read_input()
    interpreter = OptcodeInterpreter(optcode, quiet_mode=True)

    def on_input_requested():
        if not interpreter.output.empty():
            # Get status
            status = interpreter.output.get()
            requested_point = droid.get_requested_position()
            if status == 0:
                # Hit a wall
                space.explore(requested_point, POINT_WALL)
            elif status == 1:
                # Empty space, position updated
                space.explore(requested_point, POINT_EMPTY)
                droid.set_position(requested_point)
            elif status == 2:
                # Oxygen space, position updated
                space.explore(requested_point, POINT_OXYGEN)
                droid.set_oxigen_position(requested_point)
                droid.set_position(requested_point)
                print("Oxigen found at distance:", space.get_distance_from_start(requested_point))
        move = droid.act(space)
        if move is None:
            compute_oxygen_time(space, droid.get_oxygen_position())
            interpreter.input.put(0)
        else:
            interpreter.input.put(move)

    interpreter.set_input_request_listener(on_input_requested)
    interpreter.run()
Exemplo n.º 13
0
def test3():
    print("Test 3: Should print large number in the middle")
    optcode_raw = "104,1125899906842624,99"
    optcode = list(map(lambda el: int(el), optcode_raw.split(",")))
    interpreter = OptcodeInterpreter(optcode)
    interpreter.run()
Exemplo n.º 14
0
def test2():
    print("Test 2: Should print 16 bit number")
    optcode_raw = "1102,34915192,34915192,7,4,7,99,0"
    optcode = list(map(lambda el: int(el), optcode_raw.split(",")))
    interpreter = OptcodeInterpreter(optcode)
    interpreter.run()
Exemplo n.º 15
0
def test():
    print("Test 1: Should print itself")
    optcode_raw = "109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99"
    optcode = list(map(lambda el: int(el), optcode_raw.split(",")))
    interpreter = OptcodeInterpreter(optcode)
    interpreter.run()