|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +importos |
| 3 | + |
| 4 | +test_data= {} |
| 5 | + |
| 6 | +test=1 |
| 7 | +test_data[test]= {"input":"""2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2""", |
| 8 | +"expected": ['138','Unknown'], |
| 9 | + } |
| 10 | + |
| 11 | +test='real' |
| 12 | +input_file=os.path.join(os.path.dirname(__file__),'Inputs',os.path.basename(__file__).replace('.py','.txt')) |
| 13 | +test_data[test]= {"input":open(input_file,"r+").read().strip(), |
| 14 | +"expected": ['41849','32487'], |
| 15 | + } |
| 16 | + |
| 17 | +# -------------------------------- Control program execution -------------------------------- # |
| 18 | + |
| 19 | +case_to_test='real' |
| 20 | +part_to_test=2 |
| 21 | +verbose_level=1 |
| 22 | + |
| 23 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 24 | + |
| 25 | +puzzle_input=test_data[case_to_test]['input'] |
| 26 | +puzzle_expected_result=test_data[case_to_test]['expected'][part_to_test-1] |
| 27 | +puzzle_actual_result='Unknown' |
| 28 | + |
| 29 | + |
| 30 | +# -------------------------------- Actual code execution -------------------------------- # |
| 31 | + |
| 32 | +nodes= {} |
| 33 | +node_hierarchy= {} |
| 34 | + |
| 35 | +defprocess_node (data,i): |
| 36 | +globalnodes,node_hierarchy |
| 37 | +parent=i |
| 38 | +subnodes,metadata=data[i:i+2] |
| 39 | +ifsubnodes==0: |
| 40 | +nodes.update({i:data[i+2:i+2+metadata]}) |
| 41 | +i+=2+metadata |
| 42 | +returni |
| 43 | +else: |
| 44 | +i+=2 |
| 45 | +node_hierarchy[parent]=list() |
| 46 | +forjinrange (subnodes): |
| 47 | +node_hierarchy[parent].append(i) |
| 48 | +i=process_node (data,i) |
| 49 | +nodes.update({parent:data[i:i+metadata]}) |
| 50 | +i+=metadata |
| 51 | +returni |
| 52 | + |
| 53 | +defnode_value (node,node_values): |
| 54 | +globalnodes,node_hierarchy |
| 55 | +ifnodeinnode_values: |
| 56 | +returnnode_values[node] |
| 57 | +elifnodenotinnode_hierarchy: |
| 58 | +returnsum(nodes[node]) |
| 59 | +else: |
| 60 | +children= [node_hierarchy[node][child-1]forchildinnodes[node]ifchild<=len(node_hierarchy[node])] |
| 61 | +unknown_child_value=set(childforchildinchildrenifchildnotinnode_values) |
| 62 | +ifunknown_child_value: |
| 63 | +forchildinunknown_child_value: |
| 64 | +node_values[child]=node_value(child,node_values) |
| 65 | +returnsum(node_values[child]forchildinchildren) |
| 66 | + |
| 67 | +returnnode_values[node] |
| 68 | + |
| 69 | +header=True |
| 70 | + |
| 71 | +data=list(map(int,puzzle_input.split(' '))) |
| 72 | +process_node(data,0) |
| 73 | + |
| 74 | +ifpart_to_test==1: |
| 75 | +puzzle_actual_result=sum(sum(nodes.values(), [])) |
| 76 | + |
| 77 | +else: |
| 78 | +puzzle_actual_result=node_value(0, {}) |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +# -------------------------------- Outputs / results -------------------------------- # |
| 84 | + |
| 85 | +ifverbose_level>=3: |
| 86 | +print ('Input : '+puzzle_input) |
| 87 | +print ('Expected result : '+str(puzzle_expected_result)) |
| 88 | +print ('Actual result : '+str(puzzle_actual_result)) |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |