2018/08.py
def part2() -> int:
    """
    Part 2 requires is very similar, but has a few different rules for the sum
    calculation.
    """

    def traverse_sum(node):
        s = 0
        if len(node[0]) == 0:
            # If a node has no child nodes, its value is the sum of its metadata
            # entries.
            return sum(node[1])
        else:
            # However, if a node does have child nodes, the metadata entries become
            # indexes which refer to those child nodes. A metadata entry of 1 refers to
            # the first child node, 2 to the second, 3 to the third, and so on. The
            # value of this node is the sum of the values of the child nodes referenced
            # by the metadata entries. If a referenced child node does not exist, that
            # reference is skipped. A child node can be referenced multiple time and
            # counts each time it is referenced. A metadata entry of 0 does not refer to
            # any child node.

            # Go thorugh all of the metadata IDs, and if they are valid for this node,
            # add them to the sum.
            for mid in node[1]:
                if 0 < mid <= len(node[0]):
                    s += traverse_sum(node[0][mid - 1])

        return s

    return traverse_sum(TREE)

    Part 2 requires is very similar, but has a few different rules for the sum
    calculation.
    
Part 2 requires is very similar, but has a few different rules for the sum
calculation.
Part 2 requires is very similar but has a few different rules for the sum calculation .
def part2() -> int:
    def traverse_sum(node):
        s = 0
        if len(node[0]) == 0:
            return sum(node[1])
        else:
            for mid in node[1]:
                if 0 < mid <= len(node[0]):
                    s += traverse_sum(node[0][mid - 1])
        return s
    return traverse_sum(TREE)
def part2 ( ) -> int : def traverse_sum ( node ) : s = 0 if len ( node [ 0 ] ) == 0 : return sum ( node [ 1 ] ) else : for mid in node [ 1 ] : if 0 < mid <= len ( node [ 0 ] ) : s += traverse_sum ( node [ 0 ] [ mid - 1 ] ) return s return traverse_sum ( TREE )
Part 2 requires is very similar, but has a few different rules for the sum
calculation.
Part 2 requires is very similar but has a few different rules for the sum calculation .
"""
    Part 2 requires is very similar, but has a few different rules for the sum
    calculation.
    """ # If a node has no child nodes, its value is the sum of its metadata # entries. # However, if a node does have child nodes, the metadata entries become # indexes which refer to those child nodes. A metadata entry of 1 refers to # the first child node, 2 to the second, 3 to the third, and so on. The # value of this node is the sum of the values of the child nodes referenced # by the metadata entries. If a referenced child node does not exist, that # reference is skipped. A child node can be referenced multiple time and # counts each time it is referenced. A metadata entry of 0 does not refer to # any child node. # Go thorugh all of the metadata IDs, and if they are valid for this node, # add them to the sum.