internal static void Part2() { List <int> Input = EightInput.FullInput; EightTree tree = BuildTree(Input); Console.WriteLine($"Value of root node: {tree.RootValue}"); }
public static void Part1() { List <int> Input = EightInput.FullInput; EightTree tree = BuildTree(Input); Console.WriteLine($"Sum of all metadata: {tree.Sum}"); }
private static EightTree BuildTree(List <int> Input) { //int nodeLength = 1 + metadataLength + subnodeLengths; int frontIndex = 0; int sum = 0; EightNode root = new EightNode(Input[frontIndex++], Input[frontIndex++]); EightNode currentNode = root; do { if (currentNode.ChildCount > 0 && currentNode.Children.Count != currentNode.ChildCount) { // "recurse" EightNode child = new EightNode(Input[frontIndex++], Input[frontIndex++]); child.Parent = currentNode; currentNode.Children.Add(child); currentNode = child; } else if (currentNode.MetadataCount > 0 && currentNode.Metadata.Count != currentNode.MetadataCount) { // read metadata for (int i = 0; i < currentNode.MetadataCount; i++) { currentNode.Metadata.Add(Input[frontIndex++]); } sum += currentNode.Metadata.Sum(); currentNode = currentNode.Parent; } else { currentNode = currentNode.Parent; } }while (frontIndex < Input.Count); EightTree tree = new EightTree(root); tree.Sum = sum; return(tree); }