Advertisement
mgla

Advent of Code 2023 - Day 13

Dec 13th, 2023
915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. var input = File.ReadAllText("input.txt").Split("\r\n\r\n").Select(p => p.Split("\r\n")).ToList();
  2.  
  3. var part1Rows = new List<int>();
  4. var part1Cols = new List<int>();
  5.  
  6. for (var i = 0; i < input.Count; i++)
  7. {
  8.     var (rows, cols) = CheckPattern(i);
  9.     part1Rows.Add(rows);
  10.     part1Cols.Add(cols);
  11. }
  12.  
  13. Console.WriteLine($"Part 1: {part1Rows.Sum() + part1Cols.Sum()}");
  14.  
  15. var part2Rows = 0;
  16. var part2Cols = 0;
  17.  
  18. for (var i = 0; i < input.Count; i++)
  19. {
  20.     var (rows, cols) = CheckPattern(i, 1);
  21.  
  22.     part2Rows += rows;
  23.     part2Cols += cols;
  24. }
  25.  
  26. Console.WriteLine($"Part 2: {part2Rows + part2Cols}");
  27. return;
  28.  
  29. (int Rows, int Columns) CheckPattern(int patternIndex, int offset = 0)
  30. {
  31.     var pattern = input[patternIndex];
  32.     var rows = 0;
  33.     var cols = 0;
  34.    
  35.     for (var i = 1; i < pattern.Length; i++) // Check rows.
  36.     {
  37.         var rowOffset = offset;
  38.         var isMirrorRow = true;
  39.  
  40.         for (var j = 1; j <= pattern.Length - i; j++)
  41.         {
  42.             if (j > i || j + i > pattern.Length) break; // Outside of pattern bounds.
  43.  
  44.             var row1 = pattern[i + j - 1];
  45.             var row2 = pattern[i - j];
  46.  
  47.             // Count the number of differences between the two rows
  48.             var diff = row1.Select((row, rowIndex) => row == row2[rowIndex] ? 0 : 1).Sum();
  49.  
  50.             // Check if the row is a mirror of the adjacent row or the difference is within the offset.
  51.             isMirrorRow &= diff == rowOffset || diff == 0;
  52.  
  53.             if (isMirrorRow && diff == rowOffset)
  54.             {
  55.                 rowOffset = 0; // Offset already used, so set it to 0.
  56.             }
  57.         }
  58.  
  59.         // Check if all rows are mirrored and the offset, if any, has been used.
  60.         // If there is an offset, then the result should be different from the Part 1 result.
  61.         if (isMirrorRow && rowOffset == 0 && (offset == 0 || part1Rows[patternIndex] != i))
  62.         {
  63.             rows += i * 100;
  64.             break;
  65.         }
  66.     }
  67.  
  68.     for (var i = 1; i < pattern[0].Length; i++) // Check columns.
  69.     {
  70.         var columnOffset = offset;
  71.         var isMirrorColumn = true;
  72.  
  73.         for (var j = 1; j <= pattern[0].Length - i; j++)
  74.         {
  75.             if (j > i || j + i > pattern[0].Length) break; // Outside of pattern bounds.
  76.  
  77.             var col1 = pattern.Select(p => p[i + j - 1]).ToArray();
  78.             var col2 = pattern.Select(p => p[i - j]).ToArray();
  79.  
  80.             // Count the number of differences between the two columns
  81.             var diff = col1.Select((col, colIndex) => col == col2[colIndex] ? 0 : 1).Sum();
  82.  
  83.             // Check if the column is a mirror of the adjacent column or the difference is within the offset.
  84.             isMirrorColumn &= diff == columnOffset || diff == 0;
  85.  
  86.             if (isMirrorColumn && diff == columnOffset)
  87.             {
  88.                 columnOffset = 0; // Offset already used, so set it to 0.
  89.             }
  90.         }
  91.  
  92.         // Check if all columns are mirrored and the offset, if any, has been used.
  93.         // If there is an offset, then the result should be different from the Part 1 result.
  94.         if (isMirrorColumn && columnOffset == 0 &&
  95.             (offset == 0 || part1Cols[patternIndex] != i))
  96.         {
  97.             cols += i;
  98.             break;
  99.         }
  100.     }
  101.  
  102.     return (rows, cols);
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement