Beispiel #1
0
        g.add_edge(e)

        targets.add(p)


# start at the bottom until we find the first node that isn't misbalanced
# to balance everything out, we have to change the parent node of this node
bottom = programs.difference(targets).pop()
current_node = get_or_create_node(bottom)
balancer = None

print('Part 1:', bottom)

while current_node is not None:
    wrong_part = {}
    for neighbour in g.get_neighbours(current_node):
        wrong_part[neighbour] = tree_sum(neighbour)

    c = Counter(list(wrong_part.values()))

    previous_node = current_node
    current_node = None

    for neighbour in wrong_part:
        if c[wrong_part[neighbour]] == 1:
            current_node = neighbour
            break

    if current_node is not None:
        for neighbour in wrong_part:
            if c[wrong_part[neighbour]] != 1:
Beispiel #2
0
    src_node = get_or_create_node(src)

    for target in targets:
        target_node = get_or_create_node(target)
        g.add_edge_by_node(src_node, target_node)

groups = 0

while True:
    current_parent = nodes.pop()
    network = set([current_parent])
    process = set([current_parent])

    groups += 1
    while len(process) > 0:
        node = process.pop()
        for neighbour in g.get_neighbours(node):
            if neighbour not in network:
                network.add(neighbour)
                process.add(neighbour)
                nodes.remove(neighbour)

    if get_or_create_node(0) in network:
        print('Part 1:', len(network))

    if len(nodes) == 0:
        break

print('Part 2:', groups)