| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace D8._1
- {
- class Program
- {
- static void Main(string[] args)
- {
- if (args.Length < 1) return;
- if (File.Exists(args[0]) == false) return;
- var file = File.OpenText(args[0]);
- var text = file.ReadToEnd();
- var all = new List<string>(text.Split(" "));
- int result = 0;
- var root = getNode(all, ref result);
- Console.WriteLine($"Result is : {result}");
- }
- static Node getNode(List<string> all, ref int result)
- {
- var node = new Node();
- (int children, int metadatas) header = (int.Parse(all[0]), int.Parse(all[1]));
- all.RemoveAt(0);
- all.RemoveAt(0);
- for (int j = 0; j < header.children; j++)
- {
- var childnode = getNode(all, ref result);
- node.Children.Add(childnode);
- }
- for (int j = 0; j < header.metadatas; j++)
- {
- int meta = int.Parse(all[0]);
- all.RemoveAt(0);
- node.Meta.Add(meta);
- result += meta;
- }
- return node;
- }
- }
- class Node
- {
- public List<int> Meta { get; set; }
- public List<Node> Children { get; set; }
- public Node()
- {
- Meta = new List<int>();
- Children = new List<Node>();
- }
- }
- }
|