Program.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. namespace D23._1
  7. {
  8. class Program
  9. {
  10. static int Manhattan((int x, int y, int z, int) a, (int x, int y, int z, int) b)
  11. {
  12. return Math.Abs(a.x - b.x) + Math.Abs(a.y - b.y) + Math.Abs(a.z - b.z);
  13. }
  14. static void Main(string[] args)
  15. {
  16. if (args.Length < 1) return;
  17. if (File.Exists(args[0]) == false) return;
  18. var file = File.OpenText(args[0]);
  19. var reg = new Regex(@"pos=<([\d-,]+)>, r=(\d+)");
  20. var nanoList = new List<(int x, int y, int z, int r)>();
  21. (int x, int y, int z, int r) strongest = (0, 0, 0, 0);
  22. do
  23. {
  24. var line = file.ReadLine();
  25. if (line == null) break;
  26. var res = reg.Match(line);
  27. var xyz = res.Groups[1].Value.Split(",").Select(s => int.Parse(s)).ToArray();
  28. var r = int.Parse(res.Groups[2].Value);
  29. nanoList.Add((xyz[0], xyz[1], xyz[2], r));
  30. if (r > strongest.r) strongest = (xyz[0], xyz[1], xyz[2], r);
  31. } while (true);
  32. file.Close();
  33. int inRange = 0;
  34. foreach (var n in nanoList)
  35. {
  36. int m = Manhattan(strongest, n);
  37. if (m <= strongest.r) inRange += 1;
  38. }
  39. Console.WriteLine($"Answer is : {inRange}");
  40. }
  41. }
  42. }