1
0
www.mikescher.com/www/statics/aoc/2018/08-1.linq

29 lines
720 B
Plaintext
Raw Normal View History

2019-11-02 20:19:34 +01:00
<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);
MetaSum(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();