Image.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. namespace Day13;
  2. public class Image
  3. {
  4. private readonly IList<string> _lines;
  5. public Image(IList<string> lines)
  6. {
  7. _lines = lines;
  8. }
  9. public void Print()
  10. {
  11. foreach (var line in _lines)
  12. {
  13. Console.WriteLine(line);
  14. }
  15. }
  16. public int FindFold()
  17. {
  18. var map = new Dictionary<string, List<int>>();
  19. for (var i = 0; i < _lines.Count; i++)
  20. {
  21. if (!map.ContainsKey(_lines[i]))
  22. {
  23. map[_lines[i]] = new List<int>();
  24. }
  25. map[_lines[i]].Add(i + 1);
  26. }
  27. var centers = map
  28. .Where(kvp => kvp.Value.Count >= 2)
  29. .SelectMany(kvp =>
  30. Pairs(kvp.Value.Count)
  31. .Select(p =>
  32. new AxisInfo(
  33. (kvp.Value[p.Left] + kvp.Value[p.Right]) / 2,
  34. Math.Min(kvp.Value[p.Left], kvp.Value[p.Right]),
  35. Math.Max(kvp.Value[p.Left], kvp.Value[p.Right]))
  36. )
  37. .Where(info => (info.Upper - info.Lower) % 2 == 1)
  38. ).GroupBy(info => info.Center);
  39. foreach (var group in centers)
  40. {
  41. var toBorder = Math.Min(group.Key, _lines.Count - group.Key);
  42. if (group.Count() == toBorder)
  43. {
  44. return group.Key;
  45. }
  46. }
  47. return -1;
  48. }
  49. public int FindFixedFold()
  50. {
  51. var map = new Dictionary<string, List<int>>();
  52. for (var i = 0; i < _lines.Count; i++)
  53. {
  54. if (!map.ContainsKey(_lines[i]))
  55. {
  56. map[_lines[i]] = new List<int>();
  57. }
  58. map[_lines[i]].Add(i + 1);
  59. }
  60. var centers = map
  61. .Where(kvp => kvp.Value.Count >= 2)
  62. .SelectMany(kvp =>
  63. Pairs(kvp.Value.Count)
  64. .Select(p =>
  65. new AxisInfo(
  66. (kvp.Value[p.Left] + kvp.Value[p.Right]) / 2,
  67. Math.Min(kvp.Value[p.Left], kvp.Value[p.Right]),
  68. Math.Max(kvp.Value[p.Left], kvp.Value[p.Right]))
  69. )
  70. .Where(info => (info.Upper - info.Lower) % 2 == 1)
  71. ).GroupBy(info => info.Center).ToDictionary(g => g.Key, g => g.ToList());
  72. if (!centers.ContainsKey(1))
  73. {
  74. centers.Add(1, new List<AxisInfo>());
  75. }
  76. if (!centers.ContainsKey(_lines.Count - 1))
  77. {
  78. centers.Add(_lines.Count - 1, new List<AxisInfo>());
  79. }
  80. foreach (var group in centers)
  81. {
  82. var toBorder = Math.Min(group.Key, _lines.Count - group.Key);
  83. if (group.Value.Count == toBorder - 1)
  84. {
  85. if (CanFix(group))
  86. {
  87. return group.Key;
  88. }
  89. }
  90. }
  91. return -1;
  92. }
  93. public Image Rotate()
  94. {
  95. return new Image(Enumerable.Range(0, _lines[0].Length)
  96. .Select(i => new string(_lines.Select(x => x[i]).ToArray()))
  97. .ToList());
  98. }
  99. private static IEnumerable<(int Left, int Right)> Pairs(int n)
  100. {
  101. for (var i = 0; i < n - 1; i++)
  102. {
  103. for (var j = i + 1; j < n; j++)
  104. {
  105. yield return (i, j);
  106. }
  107. }
  108. }
  109. private bool CanFix(KeyValuePair<int, List<AxisInfo>> group)
  110. {
  111. for (var i = 0; i < group.Value.Count + 1; i++)
  112. {
  113. if (!group.Value.Exists(info => info.Lower == group.Key - i))
  114. {
  115. if (Distance(_lines[group.Key - i - 1], _lines[group.Key + i]) == 1)
  116. {
  117. return true;
  118. }
  119. }
  120. }
  121. return false;
  122. }
  123. private static int Distance(string left, string right)
  124. {
  125. return left.Zip(right).Count(pair => pair.First != pair.Second);
  126. }
  127. private record AxisInfo(int Center, int Lower, int Upper);
  128. }