Program.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 < 1) return;
  12. if (File.Exists(args[0]) == false) return;
  13. var coordinates = new List<(uint x, uint y)>();
  14. var file = File.OpenText(args[0]);
  15. do
  16. {
  17. var line = file.ReadLine();
  18. if (line == null) break;
  19. var cl = line.Split(", ");
  20. coordinates.Add((uint.Parse(cl[0]), uint.Parse(cl[1])));
  21. } while (true);
  22. int area = 0;
  23. for (uint x = 0; x < 500; x++)
  24. {
  25. for (uint y = 0; y < 500; y++)
  26. {
  27. var total = GetTotalDistance(coordinates, x, y);
  28. if (total < 10000) area++;
  29. }
  30. }
  31. Console.WriteLine($"Answer : {area}");
  32. }
  33. private static int GetTotalDistance(List<(uint x, uint y)> coordinates, uint x, uint y)
  34. {
  35. int total = 0;
  36. for (int i = 0; i < coordinates.Count; i++)
  37. {
  38. var coord = coordinates[i];
  39. var manhattan = Math.Abs((int)x - (int)coord.x) + Math.Abs((int)y - (int)coord.y);
  40. total += manhattan;
  41. }
  42. return total;
  43. }
  44. }
  45. }