From f6ff66ea2da1f30303f17a2dac341411fb325881 Mon Sep 17 00:00:00 2001 From: blenovo Date: Sat, 16 Aug 2025 00:38:26 +0200 Subject: final puzzle and little cleanup for 2018 --- 2018/aoc2018-d08.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 2018/aoc2018-d08.py (limited to '2018/aoc2018-d08.py') diff --git a/2018/aoc2018-d08.py b/2018/aoc2018-d08.py new file mode 100644 index 0000000..0dc174a --- /dev/null +++ b/2018/aoc2018-d08.py @@ -0,0 +1,52 @@ +#advent of code 2018 +#day 08 +#part 1 and 2 + + +f = open("08.in", 'r'); +myinput = eval(f.read().replace(" ",",")); +f.close(); + +#part 1 +def NodeAnalysis_1(License, index): + NumberOfChilds = License[index[0]]; + index[0] += 1; + NumberofEntries = License[index[0]]; + index[0] += 1; + for child in range(NumberOfChilds): + NodeAnalysis_1(License, index); + for entry in range(NumberofEntries): + index[1] += License[index[0]]; + index[0] += 1; + +#part 2 +def NodeAnalysis_2(License, index): + NodeVal = 0; + NumberOfChilds = License[index[0]]; + index[0] += 1; + NumberofEntries = License[index[0]]; + index[0] += 1; + ListOfEntries = []; + ListOfChilds = []; + for child in range(NumberOfChilds): + ListOfChilds.append(NodeAnalysis_2(License, index)); + + for entry in range(NumberofEntries): + ListOfEntries.append(License[index[0]]); + index[0] += 1; + if(NumberOfChilds == 0): + NodeVal = sum(ListOfEntries); + else: + for e in ListOfEntries: + if (e == 0 or e >NumberOfChilds): + continue; + NodeVal+= ListOfChilds[e-1]; + index.append(NodeVal); + return NodeVal; + +i = [0,0]; + +NodeAnalysis_1(myinput, i); +print("part 1 =", i[-1]); +part2 = NodeAnalysis_2(myinput, [0,0]); +print("part 2 =", part2); -- cgit v1.2.3