Program.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. namespace D6._2
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. if (args.Length < 3) return;
  12. if (File.Exists(args[0]) == false) return;
  13. if (int.TryParse(args[1], out int size) == false) return;
  14. if (int.TryParse(args[2], out int ceil) == false) return;
  15. var coordinates = new List<(uint x, uint y)>();
  16. var file = File.OpenText(args[0]);
  17. do
  18. {
  19. var line = file.ReadLine();
  20. if (line == null) break;
  21. var cl = line.Split(", ");
  22. coordinates.Add((uint.Parse(cl[0]), uint.Parse(cl[1])));
  23. } while (true);
  24. int area = 0;
  25. for (uint x = 0; x < size; x++)
  26. {
  27. for (uint y = 0; y < size; y++)
  28. {
  29. var total = GetTotalDistance(ceil, coordinates, (x, y));
  30. if (total < ceil) area++;
  31. }
  32. }
  33. Console.WriteLine($"Answer : {area}");
  34. }
  35. private static int GetTotalDistance(int ceil, List<(uint x, uint y)> coordinates, (uint x, uint y) point)
  36. {
  37. int total = 0;
  38. for (int i = 0; i < coordinates.Count; i++)
  39. {
  40. var (x, y) = coordinates[i];
  41. var manhattan = Math.Abs((int)point.x - (int)x) + Math.Abs((int)point.y - (int)y);
  42. total += manhattan;
  43. if (total >= ceil) break;
  44. }
  45. return total;
  46. }
  47. }
  48. }