1
0

Program.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. namespace D22._1
  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 file = File.OpenText(args[0]);
  14. int depth = int.Parse(file.ReadLine().Substring("depth: ".Length));
  15. int[] arrayCoord = file.ReadLine().Substring("target: ".Length).Split(",").Select(s => int.Parse(s)).ToArray();
  16. (int x, int y) coord = (arrayCoord[0], arrayCoord[1]);
  17. file.Close();
  18. Dictionary<(int, int), int> erosionLevels = new Dictionary<(int, int), int>();
  19. int risk = 0;
  20. for (int i = 0; i <= Math.Min(coord.x, coord.y); ++i)
  21. {
  22. for (int y = i; y <= coord.y; ++y)
  23. {
  24. var pos = (i, y);
  25. risk += ComputeRisk(coord, erosionLevels, pos, depth);
  26. }
  27. for (int x = i + 1; x <= coord.x; ++x)
  28. {
  29. var pos = (x, i);
  30. risk += ComputeRisk(coord, erosionLevels, pos, depth);
  31. }
  32. }
  33. Console.WriteLine($"Total risk is : {risk}");
  34. }
  35. private static int ComputeRisk((int x, int y) coord, Dictionary<(int, int), int> erosionLevels, (int x, int y) pos, int depth)
  36. {
  37. int geo = 0;
  38. if (pos != (0, 0) && pos != coord)
  39. {
  40. if (pos.x == 0) geo = pos.y * 48271;
  41. else if (pos.y == 0) geo = pos.x * 16807;
  42. else geo = erosionLevels[(pos.x - 1, pos.y)] * erosionLevels[(pos.x, pos.y - 1)];
  43. }
  44. int ero = (geo + depth) % 20183;
  45. int type = ero % 3;
  46. erosionLevels.Add(pos, ero);
  47. return type;
  48. }
  49. }
  50. }