1
0

Program.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. namespace D10._2
  8. {
  9. class Map : List<(int x, int y, int vx, int vy)> { }
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. if (args.Length < 1) return;
  15. if (File.Exists(args[0]) == false) return;
  16. var file = File.OpenText(args[0]);
  17. var r = new Regex(@"position=<\s*(?<posx>-?\d+),\s*(?<posy>-?\d+)> velocity=<\s*(?<vx>-?\d+),\s*(?<vy>-?\d+)>");
  18. var map = new Map();
  19. do
  20. {
  21. var line = file.ReadLine();
  22. if (line == null) break;
  23. var result = r.Match(line);
  24. map.Add((
  25. int.Parse(result.Groups["posx"].Value),
  26. int.Parse(result.Groups["posy"].Value),
  27. int.Parse(result.Groups["vx"].Value),
  28. int.Parse(result.Groups["vy"].Value)
  29. ));
  30. } while (true);
  31. int tt = 0;
  32. do
  33. {
  34. for (var i = 0; i < map.Count; ++i)
  35. {
  36. var star = map[i];
  37. star.x += star.vx;
  38. star.y += star.vy;
  39. map[i] = star;
  40. }
  41. tt++;
  42. } while (isOk(map) == false);
  43. Console.WriteLine($"Answer is : {tt}s");
  44. }
  45. static bool isOk(Map map)
  46. {
  47. for (var i = 0; i < map.Count; ++i)
  48. if (isIsolated(map, i)) return false;
  49. return true;
  50. }
  51. static bool isIsolated(Map map, int current)
  52. {
  53. var currentStar = map[current];
  54. foreach (var star in map)
  55. {
  56. if (star.x == currentStar.x && star.y == currentStar.y)
  57. continue;
  58. if (star.x == currentStar.x && (star.y == currentStar.y + 1 || star.y == currentStar.y - 1))
  59. return false;
  60. if (star.y == currentStar.y && (star.x == currentStar.x + 1 || star.x == currentStar.x - 1))
  61. return false;
  62. if ((star.x == currentStar.x + 1 || star.x == currentStar.x - 1) && (star.y == currentStar.y + 1 || star.y == currentStar.y - 1))
  63. return false;
  64. if ((star.y == currentStar.y + 1 || star.y == currentStar.y - 1) && (star.x == currentStar.x + 1 || star.x == currentStar.x - 1))
  65. return false;
  66. }
  67. return true;
  68. }
  69. }
  70. }