Program.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. namespace D20._1
  8. {
  9. class Program
  10. {
  11. static (int x, int y) MoveTo(char c, (int x, int y) pos)
  12. {
  13. switch (c)
  14. {
  15. case 'N': pos.y -= 1; break;
  16. case 'S': pos.y += 1; break;
  17. case 'W': pos.x -= 1; break;
  18. case 'E': pos.x += 1; break;
  19. }
  20. return pos;
  21. }
  22. static Dictionary<(int x, int y), int> makeMap(string input)
  23. {
  24. var result = new Dictionary<(int x, int y), int>();
  25. var nodes = new Stack<((int x, int y), int dist)>();
  26. ((int x, int y) pos, int dist) cur = ((0, 0), 0);
  27. foreach (var c in input)
  28. {
  29. switch (c)
  30. {
  31. case 'N':
  32. case 'S':
  33. case 'E':
  34. case 'W':
  35. cur.pos = MoveTo(c, cur.pos);
  36. cur.dist++;
  37. result.TryAdd(cur.pos, cur.dist);
  38. break;
  39. case '(':
  40. nodes.Push(cur);
  41. break;
  42. case '|':
  43. cur = nodes.Peek();
  44. break;
  45. case ')':
  46. cur = nodes.Pop();
  47. break;
  48. }
  49. }
  50. return result;
  51. }
  52. static void Main(string[] args)
  53. {
  54. if (args.Length < 1) return;
  55. if (File.Exists(args[0]) == false) return;
  56. var file = File.OpenText(args[0]);
  57. var regex = file.ReadToEnd();
  58. file.Close();
  59. //regex = @"^ENNWSWW(NEWS|)SSSEEN(WNSE|)EE(SWEN|)NNN$";
  60. //regex = @"^WSSEESWWWNW(S|NENNEEEENN(ESSSSW(NWSW|SSEN)|WSWWN(E|WWS(E|SS))))$";
  61. //regex = @"^N(EEENWWW|N)$";
  62. regex = regex.TrimStart('^').TrimEnd('$');
  63. var map = makeMap(regex);
  64. int max = map.Max(p => p.Value);
  65. int d = map.Count(p => p.Value >= 1000);
  66. Console.WriteLine($"Ex 1 : {max}\nEx 2 ; {d}");
  67. }
  68. }
  69. }