Exemplo n.º 1
0
def update_robot_if_way_blocked(robot, next_pos, go_direction,
                                invalid_positions, step_number):
    counter_clockwise = {UP: LEFT, LEFT: DOWN, DOWN: RIGHT, RIGHT: UP}
    direction1 = counter_clockwise[go_direction]
    cur_pos_direction1 = next_pos
    next_pos_direction1 = utils.calc_next_pos(cur_pos_direction1, direction1)
    while next_pos_direction1 in invalid_positions and \
            invalid_positions[next_pos_direction1].occupied_type \
            == PERMANENT_OCCUPIED:
        cur_pos_direction1 = next_pos_direction1
        next_pos_direction1 = utils.calc_next_pos(cur_pos_direction1,
                                                  direction1)

    direction2 = utils.OPPOSING_DIRECTION[counter_clockwise[go_direction]]
    cur_pos_direction2 = next_pos
    next_pos_direction2 = utils.calc_next_pos(cur_pos_direction2, direction2)
    while next_pos_direction2 in invalid_positions and invalid_positions[next_pos_direction2].occupied_type \
            == PERMANENT_OCCUPIED:
        cur_pos_direction2 = next_pos_direction2
        next_pos_direction2 = utils.calc_next_pos(cur_pos_direction2,
                                                  direction2)

    if cur_pos_direction1 != cur_pos_direction2:
        if [go_direction,
            (cur_pos_direction1, cur_pos_direction2)] not in robot.way_blocked:
            if robot.index in [18, 24] or True:
                log.info(
                    f'step {step_number} robot {robot.index} {[go_direction, (cur_pos_direction1, cur_pos_direction2)]}'
                )
            robot.way_blocked.append(
                [go_direction, (cur_pos_direction1, cur_pos_direction2)])
            robot.prev_pos = None
Exemplo n.º 2
0
def is_step_valid(robot, go_direction, invalid_positions):
    next_pos = utils.calc_next_pos(robot.current_pos, go_direction)
    if next_pos in invalid_positions and invalid_positions[
            next_pos].direction != go_direction:
        return False
    if next_pos in robot.self_block:
        return False
    return True
Exemplo n.º 3
0
def solve(infile: str, outfile: str, level=ERROR):
    global log
    global lock_explode_v2
    log = Logger(os.path.split(infile)[1], level=level)
    start_time = time.time()

    robots, obstacles, name = utils.read_scene(infile)
    invalid_positions = load_occupied_positions(robots, obstacles)
    grid = create_grid(robots, invalid_positions)
    graph = create_graph(grid, invalid_positions)
    remained_distance = update_robots_distances(robots, graph)
    start_distance = remained_distance
    log.info(f'Started! {remained_distance} distance')
    robots = sort_robots(robots)
    robots_dsts = list(map(lambda robot: robot.target_pos, robots))
    steps = []  # a data structure to hold all the moves for each robot
    step_number = 0
    total_moves = 0
    while is_not_finished(robots) and is_not_stuck(
            steps):  # while not all robots finished
        steps.append(
            dict())  # each step holds dictionary <robot_index,next_direction>
        stuck_robots = []

        for robot in robots:
            condition = (
                abs_distance(robot.target_pos, robot.current_pos) == 3 and
                (utils.calc_next_pos(robot.target_pos, RIGHT)
                 in invalid_positions and utils.calc_next_pos(
                     utils.calc_next_pos(robot.target_pos, RIGHT),
                     RIGHT) in invalid_positions
                 or utils.calc_next_pos(robot.target_pos, RIGHT) in obstacles
                 or utils.calc_next_pos(
                     utils.calc_next_pos(robot.target_pos, RIGHT),
                     RIGHT) in obstacles) and
                (utils.calc_next_pos(robot.target_pos, LEFT)
                 in invalid_positions and utils.calc_next_pos(
                     utils.calc_next_pos(robot.target_pos, LEFT),
                     LEFT) in invalid_positions
                 or utils.calc_next_pos(robot.target_pos, LEFT) in obstacles
                 or utils.calc_next_pos(
                     utils.calc_next_pos(robot.target_pos, LEFT),
                     LEFT) in obstacles) and
                (utils.calc_next_pos(robot.target_pos, DOWN)
                 in invalid_positions and utils.calc_next_pos(
                     utils.calc_next_pos(robot.target_pos, DOWN),
                     DOWN) in invalid_positions
                 or utils.calc_next_pos(robot.target_pos, DOWN) in obstacles
                 or utils.calc_next_pos(
                     utils.calc_next_pos(robot.target_pos, DOWN),
                     DOWN) in obstacles) and
                (utils.calc_next_pos(robot.target_pos, UP) in invalid_positions
                 and utils.calc_next_pos(
                     utils.calc_next_pos(robot.target_pos, UP),
                     UP) in invalid_positions
                 or utils.calc_next_pos(robot.target_pos, UP) in obstacles or
                 utils.calc_next_pos(utils.calc_next_pos(robot.target_pos, UP),
                                     UP) in obstacles))
            if condition:
                log.critical(f'CONDITION {robot.index}')

            blocked_count = sum([
                utils.calc_next_pos(robot.target_pos, RIGHT)
                in invalid_positions and invalid_positions[utils.calc_next_pos(
                    robot.target_pos,
                    RIGHT)].occupied_type == PERMANENT_OCCUPIED,
                utils.calc_next_pos(robot.target_pos, LEFT)
                in invalid_positions and invalid_positions[utils.calc_next_pos(
                    robot.target_pos,
                    LEFT)].occupied_type == PERMANENT_OCCUPIED,
                utils.calc_next_pos(robot.target_pos, UP) in invalid_positions
                and invalid_positions[utils.calc_next_pos(
                    robot.target_pos, UP)].occupied_type == PERMANENT_OCCUPIED,
                utils.calc_next_pos(robot.target_pos, DOWN)
                in invalid_positions and invalid_positions[utils.calc_next_pos(
                    robot.target_pos,
                    DOWN)].occupied_type == PERMANENT_OCCUPIED
            ])
            if blocked_count == 4 and (abs_distance(
                    robot.current_pos, robot.target_pos) == 2 or condition):
                log.critical(f'EXPLODE id={robot.index}')
                robot.stuck_count = 777
            elif (abs_distance(robot.current_pos, robot.target_pos) == 2 or condition) and \
                    blocked_count == 3 and lock_explode_v2 is None \
                    and not calc_sp(step_number, robot, invalid_positions)[1]:
                log.critical(f'EXPLODE V2 id={robot.index}')
                robot.stuck_count = 777
                lock_explode_v2 = robot.index

        stuck_hard_robots = [
            robot for robot in robots if robot.stuck_count > 10
        ][:1]
        right_robots, up_robots, down_robots, left_robots = [], [], [], []
        for robot in stuck_hard_robots:
            log.critical(
                f'STUCK HARD ROBOT {robot.index}, count={robot.stuck_count}!!!'
            )
            robot_pos = robot.current_pos if robot.stuck_count != 777 else robot.target_pos

            right_robots = []
            start_pos = robot_pos
            next_right = [
                robot for robot in robots
                if robot.current_pos == utils.calc_next_pos(start_pos, RIGHT)
            ]
            while len(next_right) > 0:
                right_robots.extend(next_right)
                start_pos = next_right[0].current_pos
                next_right = [
                    robot for robot in robots
                    if robot.current_pos == utils.calc_next_pos(
                        start_pos, RIGHT)
                ]
            left_robots = []
            start_pos = robot_pos
            next_left = [
                robot for robot in robots
                if robot.current_pos == utils.calc_next_pos(start_pos, LEFT)
            ]
            while len(next_left) > 0:
                left_robots.extend(next_left)
                start_pos = next_left[0].current_pos
                next_left = [
                    robot for robot in robots
                    if robot.current_pos == utils.calc_next_pos(
                        start_pos, LEFT)
                ]
            up_robots = []
            start_pos = robot_pos
            next_up = [
                robot for robot in robots
                if robot.current_pos == utils.calc_next_pos(start_pos, UP)
            ]
            while len(next_up) > 0:
                up_robots.extend(next_up)
                start_pos = next_up[0].current_pos
                next_up = [
                    robot for robot in robots
                    if robot.current_pos == utils.calc_next_pos(start_pos, UP)
                ]
            down_robots = []
            start_pos = robot_pos
            next_down = [
                robot for robot in robots
                if robot.current_pos == utils.calc_next_pos(start_pos, DOWN)
            ]
            while len(next_down) > 0:
                down_robots.extend(next_down)
                start_pos = next_down[0].current_pos
                next_down = [
                    robot for robot in robots
                    if robot.current_pos == utils.calc_next_pos(
                        start_pos, DOWN)
                ]
            right_robots = [r for r in right_robots if r != robot]
            up_robots = [r for r in up_robots if r != robot]
            down_robots = [r for r in down_robots if r != robot]
            left_robots = [r for r in left_robots if r != robot]
            total_moves = explode_position(robot_pos, right_robots[::-1],
                                           left_robots[::-1], up_robots[::-1],
                                           down_robots[::-1],
                                           invalid_positions, steps,
                                           step_number, total_moves,
                                           stuck_robots, robots_dsts, robots)
            robot.prev_pos = None
            robot.way_blocked = []
            robot.self_block = []
            if robot.current_pos != robot.target_pos:
                total_moves, _ = turn(robot, invalid_positions, steps,
                                      step_number, total_moves, stuck_robots,
                                      True, robots_dsts)
        turn_robots = [
            robot for robot in robots
            if robot not in (stuck_hard_robots + right_robots + up_robots +
                             down_robots + left_robots)
        ]
        for robot in turn_robots:  # move each robot accordingly to its priority
            if robot.current_pos != robot.target_pos:
                total_moves, _ = turn(robot, invalid_positions, steps,
                                      step_number, total_moves, stuck_robots,
                                      False, robots_dsts)
        for robot in stuck_robots:  # move each robot accordingly to its priority
            if robot.current_pos != robot.target_pos:
                total_moves, _ = turn(robot, invalid_positions, steps,
                                      step_number, total_moves, stuck_robots,
                                      True, robots_dsts)
        sides_robots = [
            r for r in right_robots + up_robots + down_robots + left_robots
        ]
        robots = sides_robots + [r for r in stuck_hard_robots] + \
                 [r for r in robots if r not in stuck_robots and r not in stuck_hard_robots and r not in sides_robots] + \
                 [r for r in stuck_robots if r not in stuck_hard_robots and r not in sides_robots]
        clean_invalid_position(invalid_positions)
        step_number += 1

    # after the algorithm finished, we should write the moves data structure to json file.
    utils.write_solution(steps, name, outfile)

    remained_distance = update_robots_distances(robots, graph)
    total_time = time.time() - start_time
    if not is_not_finished(robots):
        log.info(
            f'Finished! {total_time}s, {step_number} steps, {total_moves} moves, {remained_distance} distance'
        )
        root_log.warn('Success!')
        return {
            'succeed': True,
            'total_time': total_time,
            'number_of_steps': step_number,
            'number_of_moves': total_moves,
            'remained_distance': remained_distance,
            'start_distance': start_distance
        }
    else:
        log.info(
            f'Stuck! {total_time}s, {step_number} steps, {total_moves} moves, {remained_distance} distance'
        )
        return {
            'succeed': False,
            'total_time': total_time,
            'number_of_steps': step_number,
            'number_of_moves': total_moves,
            'remained_distance': remained_distance,
            'start_distance': start_distance
        }
Exemplo n.º 4
0
def explode_position(robot_pos, right_robots, left_robots, up_robots,
                     down_robots, invalid_positions, steps, step_number,
                     total_moves, stuck_robots, robots_dsts, robots):
    for right_robot in right_robots:
        right_robot_start_pos = right_robot.current_pos
        right_robot_target_pos = right_robot.target_pos
        right_robot.prev_pos = None
        right_robot.target_pos = utils.calc_next_pos(right_robot_start_pos,
                                                     RIGHT)
        explode_nearby(right_robot.current_pos, invalid_positions, robots)
        total_moves, direct = turn(right_robot, invalid_positions, steps,
                                   step_number, total_moves, stuck_robots,
                                   True, robots_dsts)
        right_robot.target_pos = right_robot_target_pos
        if right_robot.current_pos != right_robot_start_pos:
            invalid_positions[right_robot_start_pos] = Occupied(
                TEMPORARY_OCCUPIED, direct)
            invalid_positions[right_robot.current_pos] = Occupied(
                TEMPORARY_OCCUPIED, None)
            right_robot.way_blocked = []
            right_robot.self_block = []
            right_robot.prev_pos = None
        else:
            right_robot.stuck_count -= 1
    for left_robot in left_robots:
        left_robot_start_pos = left_robot.current_pos
        left_robot_target_pos = left_robot.target_pos
        left_robot.prev_pos = None
        left_robot.target_pos = utils.calc_next_pos(left_robot_start_pos, LEFT)
        explode_nearby(left_robot.current_pos, invalid_positions, robots)
        total_moves, direct = turn(left_robot, invalid_positions, steps,
                                   step_number, total_moves, stuck_robots,
                                   True, robots_dsts)
        left_robot.target_pos = left_robot_target_pos
        if left_robot.current_pos != left_robot_start_pos:
            invalid_positions[left_robot_start_pos] = Occupied(
                TEMPORARY_OCCUPIED, direct)
            invalid_positions[left_robot.current_pos] = Occupied(
                TEMPORARY_OCCUPIED, None)
            left_robot.way_blocked = []
            left_robot.self_block = []
            left_robot.prev_pos = None
        else:
            left_robot.stuck_count -= 1
    for up_robot in up_robots:
        up_robot_start_pos = up_robot.current_pos
        up_robot_target_pos = up_robot.target_pos
        up_robot.prev_pos = None
        up_robot.target_pos = utils.calc_next_pos(up_robot_start_pos, UP)
        explode_nearby(up_robot.current_pos, invalid_positions, robots)
        total_moves, direct = turn(up_robot, invalid_positions, steps,
                                   step_number, total_moves, stuck_robots,
                                   True, robots_dsts)
        up_robot.target_pos = up_robot_target_pos
        if up_robot.current_pos != up_robot_start_pos:
            invalid_positions[up_robot_start_pos] = Occupied(
                TEMPORARY_OCCUPIED, direct)
            invalid_positions[up_robot.current_pos] = Occupied(
                TEMPORARY_OCCUPIED, None)
            up_robot.way_blocked = []
            up_robot.self_block = []
            up_robot.prev_pos = None
        else:
            up_robot.stuck_count -= 1
    for down_robot in down_robots:
        down_robot_start_pos = down_robot.current_pos
        down_robot_target_pos = down_robot.target_pos
        down_robot.prev_pos = None
        down_robot.target_pos = utils.calc_next_pos(down_robot_start_pos, DOWN)
        explode_nearby(down_robot.current_pos, invalid_positions, robots)
        total_moves, direct = turn(down_robot, invalid_positions, steps,
                                   step_number, total_moves, stuck_robots,
                                   True, robots_dsts)
        down_robot.target_pos = down_robot_target_pos
        if down_robot.current_pos != down_robot_start_pos:
            invalid_positions[down_robot_start_pos] = Occupied(
                TEMPORARY_OCCUPIED, direct)
            invalid_positions[down_robot.current_pos] = Occupied(
                TEMPORARY_OCCUPIED, None)
            down_robot.way_blocked = []
            down_robot.self_block = []
            down_robot.prev_pos = None
        else:
            down_robot.stuck_count -= 1
    return total_moves
Exemplo n.º 5
0
def calc_robot_next_step(robot, invalid_positions, stuck, stuck_robots,
                         step_number, robots_dsts):
    valid_directions = list(
        filter(lambda di: is_step_valid(robot, di, invalid_positions),
               utils.OPPOSING_DIRECTION.keys()))
    blocked_directions = list(
        filter(lambda di: di not in valid_directions,
               utils.OPPOSING_DIRECTION.keys()))
    blocked_permanent = list(
        filter(
            lambda di: is_blocked_permanent(
                robot.current_pos, di, invalid_positions), blocked_directions))
    soft_blocked_permanent = list(
        filter(
            lambda di: is_soft_blocked_permanent(robot, di, invalid_positions),
            blocked_directions))
    if len(blocked_permanent
           ) == 3 and robot.current_pos not in robots_dsts and len(
               valid_directions) == 1:
        invalid_positions[robot.current_pos] = Occupied(
            PERMANENT_OCCUPIED, None, True)
        log.info(
            f'step {step_number} robot {robot.index} pos {robot.current_pos} is blocked because {blocked_permanent}'
        )
    elif (len(blocked_permanent) == 3
          or len(soft_blocked_permanent) == 3) and len(valid_directions) == 1:
        robot.self_block.append(robot.current_pos)
        log.info(
            f'step {step_number} robot self block {robot.index} pos {robot.current_pos} is blocked because {blocked_permanent}'
        )

    go_right = (robot.target_pos[0] - robot.current_pos[0]) > 0
    go_up = (robot.target_pos[1] - robot.current_pos[1]) > 0
    go_left = (robot.target_pos[0] - robot.current_pos[0]) < 0
    go_down = (robot.target_pos[1] - robot.current_pos[1]) < 0
    try_up = stuck and go_right
    try_left = stuck and go_up
    try_down = stuck and go_left
    try_right = stuck and go_down
    directions_to_check = []
    stay = False
    condition_direction_mapping = {
        RIGHT: go_right,
        UP: go_up,
        DOWN: go_down,
        LEFT: go_left
    }

    # if robot.last_move_direction in blocked_permanent \
    #         and ((abs(robot.current_pos[0] - robot.target_pos[0]) <= 1
    #               and abs(robot.current_pos[1] - robot.target_pos[1]) > 1)
    #              or (abs(robot.current_pos[1] - robot.target_pos[1]) <= 1 and abs(
    #             robot.current_pos[0] - robot.target_pos[0]) > 1)):
    #     update_robot_if_way_blocked(robot, robot.current_pos, robot.last_move_direction, invalid_positions, step_number)

    if len(
            robot.path
    ) > 0:  # using the shortest path we calculated before for this robot
        if valid_path(robot, invalid_positions):
            next_pos = robot.path[0]
            go_direction = utils.VECTOR_TO_DIRECTION[subtract_pos(
                robot.current_pos, next_pos)]
            robot.path = robot.path[1:]
            return next_pos, go_direction
        else:
            robot.path = []

    first_dir, first_cond = RIGHT, False
    if sum([go_right, go_up, go_left, go_down]) == 2:
        up_count = (robot.memory_steps[(robot.current_pos, UP)], UP, go_up)
        down_count = (robot.memory_steps[(robot.current_pos, DOWN)], DOWN,
                      go_down)
        right_count = (robot.memory_steps[(robot.current_pos, RIGHT)], RIGHT,
                       go_right)
        left_count = (robot.memory_steps[(robot.current_pos, LEFT)], LEFT,
                      go_left)
        next_dir = list(
            filter(lambda t: t[2],
                   [up_count, down_count, right_count, left_count]))
        if next_dir[0][0] < next_dir[1][0]:
            first_dir, first_cond = next_dir[0][1], True
        elif next_dir[0][0] > next_dir[1][0]:
            first_dir, first_cond = next_dir[1][1], True
    for go_condition, go_direction in zip([
            first_cond, condition_direction_mapping[robot.last_move_direction],
            go_right, go_up, go_left, go_down
    ], [first_dir, robot.last_move_direction, RIGHT, UP, LEFT, DOWN]):
        if go_condition:
            next_pos = utils.calc_next_pos(robot.current_pos, go_direction)
            if is_step_valid(robot, go_direction, invalid_positions):
                if next_pos != robot.prev_pos and is_way_not_blocked(
                        robot, next_pos, go_direction):
                    robot.memory_steps[(robot.current_pos, go_direction)] += 1
                    return next_pos, go_direction
            if next_pos in invalid_positions and invalid_positions[
                    next_pos].direction is not None:
                stay = True
            if (next_pos in invalid_positions and invalid_positions[next_pos].occupied_type == PERMANENT_OCCUPIED) or \
                    next_pos in robot.self_block:
                update_robot_if_way_blocked(robot, next_pos, go_direction,
                                            invalid_positions, step_number)
                update_robot_if_way_blocked_special(robot, next_pos,
                                                    go_direction,
                                                    invalid_positions,
                                                    step_number)

    if not stay:
        for go_condition, go_direction in zip(
            [try_up, try_left, try_down, try_right], [UP, LEFT, DOWN, RIGHT]):
            if go_condition:
                directions_to_check.append(go_direction)
                next_pos = utils.calc_next_pos(robot.current_pos, go_direction)
                if is_step_valid(robot, go_direction, invalid_positions):
                    if is_way_not_blocked(robot, next_pos, go_direction):
                        if next_pos != robot.prev_pos:
                            return next_pos, go_direction
                        if abs_distance(next_pos, robot.target_pos) > abs_distance(robot.current_pos, robot.target_pos) \
                                and robot.get_backs[next_pos] < 3:
                            if len(valid_directions) > 1:
                                robot.get_backs[next_pos] += 1
                            return next_pos, go_direction
                if (next_pos in invalid_positions and invalid_positions[next_pos].occupied_type == PERMANENT_OCCUPIED) \
                        or next_pos in robot.self_block:
                    update_robot_if_way_blocked_special(
                        robot, next_pos, go_direction, invalid_positions,
                        step_number)

        for direction in directions_to_check:
            go_direction = utils.OPPOSING_DIRECTION[direction]
            next_pos = utils.calc_next_pos(robot.current_pos, go_direction)
            if is_step_valid(robot, go_direction, invalid_positions):
                if is_way_not_blocked(robot, next_pos, go_direction):
                    if next_pos != robot.prev_pos:
                        return next_pos, go_direction
                    if abs_distance(next_pos, robot.target_pos) > abs_distance(robot.current_pos, robot.target_pos) \
                            and robot.get_backs[next_pos] < 3:
                        if len(valid_directions) > 1:
                            robot.get_backs[next_pos] += 1
                        return next_pos, go_direction

            if len(valid_directions) == 1:
                return utils.calc_next_pos(
                    robot.current_pos,
                    valid_directions[0]), valid_directions[0]

            if len(valid_directions) == 2:
                for go_direction in valid_directions:
                    next_pos = utils.calc_next_pos(robot.current_pos,
                                                   go_direction)
                    if is_way_not_blocked(
                            robot, next_pos,
                            go_direction) and robot.get_backs[next_pos] < 3:
                        return next_pos, go_direction
                for go_direction in valid_directions:
                    next_pos = utils.calc_next_pos(robot.current_pos,
                                                   go_direction)
                    if is_way_not_blocked(robot, next_pos, go_direction):
                        return next_pos, go_direction

        if len(valid_directions
               ) == 1 and robot.current_pos != robot.target_pos:
            return utils.calc_next_pos(
                robot.current_pos, valid_directions[0]), valid_directions[0]

        # cur_blocked_directions = get_all_blocked_directions(robot, blocked_permanent)
        # if len(cur_blocked_directions) >= 3 and len(valid_directions) > 0:
        #     log.warn(f'Calc SP for robot {robot.index}, step_number={step_number}')
        #     next_pos, go_direction = calc_sp(robot, invalid_positions)
        #     return next_pos, go_direction
    if stuck and robot.index == 34:
        log.info(
            f'Step {step_number} robot {robot.index} stuck. current {robot.current_pos} target {robot.target_pos}. directions {valid_directions} are valid'
        )

    return robot.current_pos, None
Exemplo n.º 6
0
def update_robot_if_way_blocked_special(robot, next_pos, go_direction,
                                        invalid_positions, step_number):
    counter_clockwise = {UP: LEFT, LEFT: DOWN, DOWN: RIGHT, RIGHT: UP}
    direction1 = counter_clockwise[go_direction]
    cur_pos_direction1 = next_pos
    next_pos_direction1 = utils.calc_next_pos(
        utils.calc_next_pos(cur_pos_direction1, direction1), go_direction)
    while next_pos_direction1 in invalid_positions and \
            invalid_positions[next_pos_direction1].occupied_type \
            == PERMANENT_OCCUPIED:
        cur_pos_direction1 = next_pos_direction1
        next_pos_direction1 = utils.calc_next_pos(
            utils.calc_next_pos(cur_pos_direction1, direction1), go_direction)

    direction2 = utils.OPPOSING_DIRECTION[counter_clockwise[go_direction]]
    cur_pos_direction2 = next_pos
    next_pos_direction2 = utils.calc_next_pos(
        utils.calc_next_pos(cur_pos_direction2, direction2),
        utils.OPPOSING_DIRECTION[go_direction])
    while next_pos_direction2 in invalid_positions and invalid_positions[next_pos_direction2].occupied_type \
            == PERMANENT_OCCUPIED:
        cur_pos_direction2 = next_pos_direction2
        next_pos_direction2 = utils.calc_next_pos(
            utils.calc_next_pos(cur_pos_direction2, direction2),
            utils.OPPOSING_DIRECTION[go_direction])

    if cur_pos_direction1 != cur_pos_direction2:
        if [go_direction, (cur_pos_direction1, cur_pos_direction2)
            ] not in robot.way_blocked_special:
            if robot.index in [18, 24] or True:
                log.info(
                    f'step {step_number} robot {robot.index} {[go_direction, (cur_pos_direction1, cur_pos_direction2)]}'
                )
            robot.way_blocked_special.append(
                [go_direction, (cur_pos_direction1, cur_pos_direction2)])
        if robot.get_backs[robot.current_pos] >= 3:
            if go_direction == UP:
                new = [
                    go_direction,
                    ((cur_pos_direction1[0],
                      max(cur_pos_direction1[1], cur_pos_direction2[1])),
                     (cur_pos_direction2[0],
                      max(cur_pos_direction1[1], cur_pos_direction2[1])))
                ]
            if go_direction == DOWN:
                new = [
                    go_direction,
                    ((cur_pos_direction1[0],
                      min(cur_pos_direction1[1], cur_pos_direction2[1])),
                     (cur_pos_direction2[0],
                      min(cur_pos_direction1[1], cur_pos_direction2[1])))
                ]
            if go_direction == RIGHT:
                new = [
                    go_direction,
                    ((max(cur_pos_direction1[0],
                          cur_pos_direction2[0]), cur_pos_direction1[1]),
                     (max(cur_pos_direction1[0],
                          cur_pos_direction2[0]), cur_pos_direction2[1]))
                ]
            if go_direction == LEFT:
                new = [
                    go_direction,
                    ((min(cur_pos_direction1[0],
                          cur_pos_direction2[0]), cur_pos_direction1[1]),
                     (min(cur_pos_direction1[0],
                          cur_pos_direction2[0]), cur_pos_direction2[1]))
                ]
            if new not in robot.way_blocked:
                robot.way_blocked.append(new)
            robot.prev_pos = None
Exemplo n.º 7
0
def is_soft_blocked_permanent(robot, go_direction, invalid_positions):
    next_pos = utils.calc_next_pos(robot.current_pos, go_direction)
    return is_blocked_permanent(
        robot.current_pos, go_direction,
        invalid_positions) or next_pos in robot.self_block
Exemplo n.º 8
0
def is_blocked_permanent(current_pos, go_direction, invalid_positions):
    next_pos = utils.calc_next_pos(current_pos, go_direction)
    return next_pos in invalid_positions and invalid_positions[
        next_pos].occupied_type == PERMANENT_OCCUPIED