Program.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace D01._2
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. if (args.Length < 1) throw new ArgumentException();
  11. if (File.Exists(args[0]) == false) throw new FileNotFoundException();
  12. string input = File.ReadAllText(args[0]);
  13. var visited = new HashSet<(int, int)>()
  14. {
  15. (0, 0)
  16. };
  17. (int x, int y, int r) coord = (0, 0, 0);
  18. foreach (var i in input.Split(", "))
  19. {
  20. int n = int.Parse(i.Substring(1));
  21. switch (i[0])
  22. {
  23. case 'R': coord.r += 90; break;
  24. case 'L': coord.r -= 90; break;
  25. }
  26. if (coord.r < 0) coord.r += 360;
  27. if (coord.r >= 360) coord.r -= 360;
  28. if (Visit(ref coord, n, visited) == false)
  29. break;
  30. }
  31. var manhattan = Math.Abs(coord.x) + Math.Abs(coord.y);
  32. Console.WriteLine($"Distance is : {manhattan}");
  33. }
  34. private static bool Visit(ref (int x, int y, int r) coord, int n, HashSet<(int, int)> visited)
  35. {
  36. for (var i = 0; i < n; ++i)
  37. {
  38. switch (coord.r)
  39. {
  40. case 0: coord.y --; break;
  41. case 90: coord.x ++; break;
  42. case 180: coord.y ++; break;
  43. case 270: coord.x --; break;
  44. }
  45. if (visited.Add((coord.x, coord.y)) == false)
  46. return false;
  47. }
  48. return true;
  49. }
  50. private static void HashSet(int v1, object x, int v2, object y, int v3, object r)
  51. {
  52. throw new NotImplementedException();
  53. }
  54. }
  55. }