Program.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace D8._1
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. if (args.Length < 1) return;
  11. if (File.Exists(args[0]) == false) return;
  12. var file = File.OpenText(args[0]);
  13. var text = file.ReadToEnd();
  14. var all = new List<string>(text.Split(" "));
  15. int result = 0;
  16. var root = getNode(all, ref result);
  17. Console.WriteLine($"Result is : {result}");
  18. }
  19. static Node getNode(List<string> all, ref int result)
  20. {
  21. var node = new Node();
  22. (int children, int metadatas) header = (int.Parse(all[0]), int.Parse(all[1]));
  23. all.RemoveAt(0);
  24. all.RemoveAt(0);
  25. for (int j = 0; j < header.children; j++)
  26. {
  27. var childnode = getNode(all, ref result);
  28. node.Children.Add(childnode);
  29. }
  30. for (int j = 0; j < header.metadatas; j++)
  31. {
  32. int meta = int.Parse(all[0]);
  33. all.RemoveAt(0);
  34. node.Meta.Add(meta);
  35. result += meta;
  36. }
  37. return node;
  38. }
  39. }
  40. class Node
  41. {
  42. public List<int> Meta { get; set; }
  43. public List<Node> Children { get; set; }
  44. public Node()
  45. {
  46. Meta = new List<int>();
  47. Children = new List<Node>();
  48. }
  49. }
  50. }