Program.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.IO;
  3. namespace D18._1
  4. {
  5. class Program
  6. {
  7. enum EnumType { Open = 0, Trees, Lumber }
  8. static void Main(string[] args)
  9. {
  10. if (args.Length < 1) return;
  11. if (File.Exists(args[0]) == false) return;
  12. var file = File.OpenText(args[0]);
  13. const int size = 50;
  14. var map = new (EnumType type, (int o, int t, int l) adj)[size, size];
  15. CreateMap(file, map);
  16. (int o, int t, int l) count = (0, 0, 0);
  17. int minutes = 1;
  18. do
  19. {
  20. FillAdjacency(size, map);
  21. count = (0, 0, 0);
  22. for (var y = 0; y < size; ++y)
  23. {
  24. for (var x = 0; x < size; ++x)
  25. {
  26. var tile = map[y, x];
  27. if (tile.type == EnumType.Open)
  28. {
  29. if (tile.adj.t >= 3) map[y, x].type = EnumType.Trees;
  30. }
  31. else if (tile.type == EnumType.Trees)
  32. {
  33. if (tile.adj.l >= 3) map[y, x].type = EnumType.Lumber;
  34. }
  35. else if (tile.type == EnumType.Lumber)
  36. {
  37. if (tile.adj.l >= 1 && tile.adj.t >= 1) map[y, x].type = EnumType.Lumber;
  38. else map[y, x].type = EnumType.Open;
  39. }
  40. if (map[y, x].type == EnumType.Open) count.o++;
  41. if (map[y, x].type == EnumType.Trees) count.t++;
  42. if (map[y, x].type == EnumType.Lumber) count.l++;
  43. }
  44. }
  45. } while (minutes++ < 10);
  46. int result = count.l * count.t;
  47. Console.WriteLine($"The result is : {result}");
  48. }
  49. private static void FillAdjacency(int size, (EnumType type, (int o, int t, int l) adj)[,] map)
  50. {
  51. for (var y = 0; y < size; ++y)
  52. for (var x = 0; x < size; ++x)
  53. {
  54. int count1 = OfType(EnumType.Open, size, map, y, x);
  55. int count2 = OfType(EnumType.Trees, size, map, y, x);
  56. int count3 = OfType(EnumType.Lumber, size, map, y, x);
  57. var result = (count1, count2, count3);
  58. map[y, x].adj = result;
  59. }
  60. }
  61. private static int OfType(EnumType type, int size, (EnumType type, (int o, int t, int l) adj)[,] map, int y, int x)
  62. {
  63. int count = 0;
  64. if (y > 0 && x > 0) count += map[y - 1, x - 1].type == type ? 1 : 0;
  65. if (y > 0) count += map[y - 1, x].type == type ? 1 : 0;
  66. if (y > 0 && x < size-1) count += map[y - 1, x + 1].type == type ? 1 : 0;
  67. if (x > 0) count += map[y, x - 1].type == type ? 1 : 0;
  68. if (x < size-1) count += map[y, x + 1].type == type ? 1 : 0;
  69. if (y < size-1 && x > 0) count += map[y + 1, x - 1].type == type ? 1 : 0;
  70. if (y < size-1) count += map[y + 1, x].type == type ? 1 : 0;
  71. if (y < size-1 && x < size-1) count += map[y + 1, x + 1].type == type ? 1 : 0;
  72. return count;
  73. }
  74. private static void CreateMap(StreamReader file, (EnumType type, (int o, int t, int l) adj)[,] map)
  75. {
  76. int y = 0;
  77. do
  78. {
  79. var line = file.ReadLine();
  80. if (line == null) break;
  81. int x = 0;
  82. foreach (var c in line)
  83. {
  84. EnumType type;
  85. switch (c)
  86. {
  87. case '|':
  88. type = EnumType.Trees;
  89. break;
  90. case '#':
  91. type = EnumType.Lumber;
  92. break;
  93. case '.':
  94. default:
  95. type = EnumType.Open;
  96. break;
  97. }
  98. map[y, x] = (type, (0, 0, 0));
  99. x++;
  100. }
  101. ++y;
  102. } while (true);
  103. }
  104. }
  105. }