Day08 part 2
This commit is contained in:
@@ -14,5 +14,15 @@ namespace AdventOfCode2018.Tests
|
||||
|
||||
Assert.AreEqual("138", result);
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void ResolvePart2__Test()
|
||||
{
|
||||
Day08 day = new Day08();
|
||||
|
||||
string result = day.ResolvePart2(new string[] { "2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2", });
|
||||
|
||||
Assert.AreEqual("66", result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,6 +41,25 @@ namespace AdventOfCode2018
|
||||
|
||||
What is the sum of all metadata entries?
|
||||
|
||||
--- Part Two ---
|
||||
|
||||
The second check is slightly more complicated: you need to find the value of the root node (A in the example above).
|
||||
|
||||
The value of a node depends on whether it has child nodes.
|
||||
|
||||
If a node has no child nodes, its value is the sum of its metadata entries. So, the value of node B is 10+11+12=33, and the value of node D is 99.
|
||||
|
||||
However, if a node does have child nodes, the metadata entries become indexes which refer to those child nodes. A metadata entry of 1 refers to the first child node, 2 to the second, 3 to the third, and so on. The value of this node is the sum of the values of the child nodes referenced by the metadata entries. If a referenced child node does not exist, that reference is skipped. A child node can be referenced multiple time and counts each time it is referenced. A metadata entry of 0 does not refer to any child node.
|
||||
|
||||
For example, again using the above nodes:
|
||||
|
||||
Node C has one metadata entry, 2. Because node C has only one child node, 2 references a child node which does not exist, and so the value of node C is 0.
|
||||
Node A has three metadata entries: 1, 1, and 2. The 1 references node A's first child node, B, and the 2 references node A's second child node, C. Because node B has a value of 33 and node C has a value of 0, the value of node A is 33+33+0=66.
|
||||
|
||||
So, in this example, the value of the root node is 66.
|
||||
|
||||
What is the value of the root node?
|
||||
|
||||
*/
|
||||
|
||||
public class Day08 : IDay
|
||||
@@ -55,9 +74,13 @@ namespace AdventOfCode2018
|
||||
|
||||
public string ResolvePart2(string[] inputs)
|
||||
{
|
||||
return null;
|
||||
IntStream values = new IntStream(inputs[0]);
|
||||
ChronoLicenceNode licenceTree = ChronoLicenceNode.BuildFromIntStream(values);
|
||||
int result = licenceTree.GetValue();
|
||||
return result.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public class IntStream
|
||||
{
|
||||
private int[] values;
|
||||
@@ -66,7 +89,7 @@ namespace AdventOfCode2018
|
||||
public IntStream(string strValues)
|
||||
{
|
||||
values = strValues
|
||||
.Split(new string[] { " ", "" }, StringSplitOptions.RemoveEmptyEntries)
|
||||
.Split(new string[] { " ", }, StringSplitOptions.RemoveEmptyEntries)
|
||||
.Select(strVal => Convert.ToInt32(strVal))
|
||||
.ToArray();
|
||||
index = 0;
|
||||
@@ -115,5 +138,24 @@ namespace AdventOfCode2018
|
||||
}
|
||||
return checksum;
|
||||
}
|
||||
|
||||
public int GetValue()
|
||||
{
|
||||
int value = 0;
|
||||
if (Childs.Count == 0)
|
||||
{
|
||||
value = Metadata.Sum();
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (int metadata in Metadata)
|
||||
{
|
||||
int index = metadata - 1;
|
||||
if (index < 0 || index >= Childs.Count) { continue; }
|
||||
value += Childs[index].GetValue();
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user