1
0
www.mikescher.com/www/statics/aoc/2018/08_solution-2.linq
2019-12-02 14:27:20 +01:00

31 lines
890 B
C#

<Query Kind="Program" />
class Node { public List<Node> Children = new List<Node>(); public List<int> Metadata = new List<int>(); }
void Main()
{
var data = File
.ReadAllText(Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath), @"08_input.txt"))
.Split(' ')
.Select(int.Parse)
.ToList();
int i = 0;
var root = Parse(data, ref i);
NodeVal(root).Dump();
}
private Node Parse(List<int> data, ref int pos)
{
var qn = data[pos++];
var qm = data[pos++];
var n = new Node();
for (int i = 0; i < qn; i++) n.Children.Add(Parse(data, ref pos));
for (int i = 0; i < qm; i++) n.Metadata.Add(data[pos++]);
return n;
}
private int MetaSum(Node n) => n.Metadata.Sum() + n.Children.Select(MetaSum).Sum();
private int NodeVal(Node n) => n.Children.Any() ? n.Metadata.Select(i=>i-1).Where(i=>i>=0&&i<n.Children.Count).Select(i=>NodeVal(n.Children[i])).Sum() : n.Metadata.Sum();