My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 36 lines 797 B view raw
1import fileinput 2 3 4def parse_node(nodes, i=0, part_2=False): 5 value = 0 6 start = i 7 num_children, num_meta = nodes[i:i+2] 8 child_vals = [] 9 10 i += 2 11 12 for _ in range(num_children): 13 j, val = parse_node(nodes, i, part_2) 14 child_vals.append(val) 15 if not part_2: 16 value += val 17 i += j 18 19 for j in range(num_meta): 20 if part_2 and num_children > 0: 21 try: 22 value += child_vals[nodes[i] - 1] 23 except IndexError: 24 pass 25 else: 26 value += nodes[i] 27 28 i += 1 29 30 return i - start, value 31 32 33nodes = [int(x) for x in fileinput.input()[0].split()] 34 35print "Sum of metadata entries:", parse_node(nodes)[1] 36print "Value of the root node:", parse_node(nodes, part_2=True)[1]