def main():
    map_ = read_file_as_lines("input_10.txt")
    asteroids = parse_asteroids(map_)
    location, p1 = best_monitoring_station_location(asteroids)
    vaporized = vaporize(location, asteroids)
    p2 = vaporized[199].coordinate.x * 100 + vaporized[199].coordinate.y
    print(f'part 1: {p1}, part 2: {p2}')
def main():
    input_ = read_file_as_lines("input_12.txt")
    positions = [parse_position(line) for line in input_]
    moons = [Moon(pos, Axes(0, 0, 0)) for pos in positions]
    p1 = total_system_energy(moons)
    p2 = ""
    print(f'part 1: {p1}, part 2: {p2}')
Exemple #3
0
def perform_operation(service_data, action):
    service_type = service_data.get("serviceType")
    service_name = service_data.get("name")

    successfully_configured_nodes = []
    failed_to_configure_nodes = []
    rollback_action = None
    system_attributes = {}

    is_success = True
    if service_type == "l3vpn":
        system_attributes = service_data.get("service-attributes")
        sites = service_data.get("requestData").get("l3vpn-svc").get(
            "sites").get("site")

        #system_attributes["policy_name"] = reservation_manager.get_policy_name()
        #system_attributes["ospf_id"] = str(reservation_manager.get_ospf_id())
        #system_attributes["domain_id"] = str(reservation_manager.get_domain_id())
        #system_attributes["route_target"] = reservation_manager.get_route_target()
        #system_attributes["route_distinguisher"] = reservation_manager.get_route_distinguisher()

        for site in sites:
            router_name = site.get("site-network-accesses").get(
                "site-network-access").get("provider-edge").get("router-name")
            device_info = inventory_manager.get_device_information(router_name)
            node_type = device_info.get("type")

            print "Router name (%s), Type (%s)" % (router_name, node_type)
            #system_attributes["vrf_name"] = reservation_manager.get_vrf_name(router_name, service_name)
            #system_attributes["local_as_number"] = str(reservation_manager.get_local_as_number(router_name))

            input_data = {}
            input_data["service-attributes"] = system_attributes
            input_data["site"] = site

            request_file = template_manager.generate_request_data(
                node_type, service_type, action, input_data)
            if node_type.lower() == "junos":
                command_data = utils.read_file(request_file)
                print "=========================================================="
                print command_data
                print "=========================================================="
                is_success = netconf_client.edit_config(
                    device_info, command_data)
            elif node_type.lower() == "iosxr":
                command_data = utils.read_file_as_lines(request_file)
                print "=========================================================="
                print command_data
                print "=========================================================="
                is_success = cli_client.edit_config(device_info, command_data)
            elif node_type.lower() == "iosxe":
                command_data = utils.read_file_as_lines(request_file)
                print "=========================================================="
                print command_data
                print "=========================================================="
                is_success = iosxe_cli_client.edit_config(
                    device_info, command_data)
                if not is_success:
                    if action == "activate":
                        rollback_action = "deactivate"
                    else:
                        rollbacl_action = "activate"
                    request_file = template_manager.generate_request_data(
                        node_type, service_type, rollback_action, input_data)
                    iosxe_cli_client.edit_config(device_info, command_data)
            if is_success:
                successfully_configured_nodes.append(router_name)
            else:
                failed_to_configure_nodes.append(router_name)
                break

    elif service_type == "tunnel":
        router_name = service_data.get("requestData").get("source")
        device_info = inventory_manager.get_device_information(router_name)
        node_type = device_info.get("type")
        print "Router name (%s), Type (%s)" % (router_name, node_type)
        input_data = service_data.get("requestData")
        request_file = template_manager.generate_request_data(
            node_type, service_type, action, input_data)

        if node_type.lower() == "junos":
            command_data = utils.read_file(request_file)
            is_success = netconf_client.edit_config(device_info, command_data)
        elif node_type.lower() == "iosxr":
            command_data = utils.read_file_as_lines(request_file)
            print "=========================================================="
            print command_data
            print "=========================================================="
            is_success = cli_client.edit_config(device_info, command_data)
        elif node_type.lower() == "iosxe":
            print "=========================================================="
            print "Tunnel service not supported for iosxe device"
            print "=========================================================="
            return False

        if is_success:
            successfully_configured_nodes.append(router_name)
        else:
            failed_to_configure_nodes.append(router_name)

    print "=========================================================="
    print "Successfully configured nodes: %s" % successfully_configured_nodes
    print "Failed to configure nodes: %s" % failed_to_configure_nodes
    print "=========================================================="
    if not is_success:
        if action == "activate":
            rollback_action = "deactivate"
        else:
            rollback_action = "activate"

        rollback(successfully_configured_nodes, service_data, rollback_action,
                 system_attributes)
        return False

    return True
Exemple #4
0
def rollback(nodes, service_data, rollback_action, system_attributes):
    print "Rollback for the nodes: %s" % nodes
    service_type = service_data.get("serviceType")
    service_name = service_data.get("name")

    if service_type == "l3vpn":
        sites = service_data.get("requestData").get("l3vpn-svc").get(
            "sites").get("site")

        for site in sites:
            router_name = site.get("site-network-accesses").get(
                "site-network-access").get("provider-edge").get("router-name")
            if router_name not in nodes:
                continue

            device_info = inventory_manager.get_device_information(router_name)
            node_type = device_info.get("type")
            print "Router name (%s), Type (%s)" % (router_name, node_type)
            #system_attributes["vrf_name"] = reservation_manager.get_vrf_name(router_name, service_name)
            #system_attributes["local_as_number"] = str(reservation_manager.get_local_as_number(router_name))

            input_data = {}
            input_data["service-attributes"] = system_attributes
            input_data["site"] = site

            request_file = template_manager.generate_request_data(
                node_type, service_type, rollback_action, input_data)
            if node_type.lower() == "junos":
                command_data = utils.read_file(request_file)
                print "=========================================================="
                print command_data
                print "=========================================================="
                netconf_client.edit_config(device_info, command_data)
            elif node_type.lower() == "iosxr":
                command_data = utils.read_file_as_lines(request_file)
                print "=========================================================="
                print command_data
                print "=========================================================="
                cli_client.edit_config(device_info, command_data)
            elif node_type.lower() == "iosxe":
                command_data = utils.read_file_as_lines(request_file)
                print "=========================================================="
                print command_data
                print "=========================================================="
                iosxe_cli_client.edit_config(device_info, command_data)
    elif service_type == "tunnel":
        router_name = service_data.get("requestData").get("source")
        device_info = inventory_manager.get_device_information(router_name)
        node_type = device_info.get("type")
        print "Router name (%s), Type (%s)" % (router_name, node_type)
        request_file = template_manager.generate_request_data(
            node_type, service_type, rollback_action, service_data)

        if node_type.lower() == "junos":
            command_data = utils.read_file(request_file)
            netconf_client.edit_config(device_info, command_data)
        elif node_type.lower() == "iosxr":
            command_data = utils.read_file_as_lines(request_file)
            print "=========================================================="
            print command_data
            print "=========================================================="
            cli_client.edit_config(device_info, command_data)
        elif node_type.lower() == "iosxe":
            print "=========================================================="
            print "Tunnel service not supported for iosxe device"
            print "=========================================================="
Exemple #5
0
data_dir = config.data_dir
output_dir = data_dir + "/adj_matrix"

training_file = data_dir + "/train.txt"
validate_file = data_dir + "/validate.txt"
print(
    "***************************************************************************************"
)
print("Output Dir: " + output_dir)

print("@Create output directory")
utils.create_folder(output_dir)

# Load train, validate & test
print("@Load train,validate&test data")
training_instances = utils.read_file_as_lines(training_file)
nb_train = len(training_instances)
print(" + Total training sequences: ", nb_train)

validate_instances = utils.read_file_as_lines(validate_file)
nb_validate = len(validate_instances)
print(" + Total validating sequences: ", nb_validate)

# Create dictionary
print("@Build knowledge")
MAX_SEQ_LENGTH, item_dict, reversed_item_dict = utils.build_knowledge(
    training_instances, validate_instances)

print("#Statistic")
NB_ITEMS = len(item_dict)
print(" + Maximum sequence length: ", MAX_SEQ_LENGTH)
def main():
    masses = list(map(int, read_file_as_lines("input_1.txt")))
    p1 = total_fuel_part_1(masses)
    p2 = total_fuel_part_2(masses)
    print(f'part 1: {p1}, part 2: {p2}')
Exemple #7
0
def main():
    wires = [line.split(",") for line in read_file_as_lines("input_3.txt")]
    p1 = closest_intersection_part_1(wires)
    p2 = closest_intersection_part_2(wires)
    print(f'part 1: {p1}, part 2: {p2}')
Exemple #8
0
def main():
    map_of_orbits_1, map_of_orbits_2 = itertools.tee(read_file_as_lines("input_6.txt"))
    p1 = orbit_count_checksums(map_of_orbits_1)
    p2 = minimum_orbital_transfers(map_of_orbits_2)
    print(f'part 1: {p1}, part 2: {p2}')