1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
currentfilename = "input.txt";
#currentfilename = "testinput.txt"; #example only
f = open(currentfilename, 'r');
myinput = eval(f.read().replace(" ",","));
f.close();
#for i in myinput:
# print(i);
def NodeAnalysis(License, index):
NodeVal = 0;
NumberOfChilds = License[index[0]];
index[0] += 1;
NumberofEntries = License[index[0]];
index[0] += 1;
print(index, " - this node has ", NumberOfChilds, " and ", NumberofEntries);
ListOfEntries = [];
ListOfChilds = [];
for child in range(NumberOfChilds):
ListOfChilds.append(NodeAnalysis(License, index));
for entry in range(NumberofEntries):
ListOfEntries.append(License[index[0]]);
#index[1] += License[index[0]];
index[0] += 1;
#print("\tcurrent index ", index[0]);
if(NumberOfChilds == 0):
NodeVal = sum(ListOfEntries);
#for entry in range(NumberofEntries):
# NodeVal += License[index[0]];
# index[0] += 1;
else:
for e in ListOfEntries:
if (e == 0 or e >NumberOfChilds):
continue;
NodeVal+= ListOfChilds[e-1];
print("now ", NodeVal);
index.append(NodeVal);
return NodeVal;
i = [0,0];
part2 = NodeAnalysis(myinput, i);
print("input length\t", len(myinput));
print("final index\t", i[0]);
print("part1 answer\t", i[1]);
print("part2 answer\t", i[1]);
print("part2 answer\t", i[-1]);
|