Program.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace D03._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 text = "";
  13. using (var file = File.OpenText(args[0]))
  14. {
  15. text = file.ReadToEnd();
  16. }
  17. (int x, int y)[] pos = new[] { (0, 0), (0, 0) };
  18. var visited = new HashSet<(int, int)>
  19. {
  20. (0, 0)
  21. };
  22. int santas = 2, santaId = 0;
  23. foreach (var c in text)
  24. {
  25. santaId = (santaId + 1) % santas;
  26. switch (c)
  27. {
  28. case '>': pos[santaId].x++; break;
  29. case '<': pos[santaId].x--; break;
  30. case '^': pos[santaId].y--; break;
  31. case 'v': case 'V': pos[santaId].y++; break;
  32. default: continue;
  33. }
  34. visited.Add(pos[santaId]);
  35. }
  36. Console.WriteLine($"Answer is : {visited.Count}");
  37. }
  38. }
  39. }