Advertisement
mgla

Advent of Code 2024 - Day 4

Dec 4th, 2024 (edited)
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. var input = await File.ReadAllLinesAsync("input.txt");
  2.  
  3. var gridSize = input.Length;
  4. var coordinatesX = new List<Position>();
  5. var coordinatesA = new List<Position>();
  6.  
  7. for (var row = 0; row < gridSize; row++)
  8. {
  9.     for (var col = 0; col < gridSize; col++)
  10.     {
  11.         switch (input[row][col])
  12.         {
  13.             case 'X':
  14.                 coordinatesX.Add(new Position(row, col));
  15.                 break;
  16.             case 'A':
  17.                 coordinatesA.Add(new Position(row, col));
  18.                 break;
  19.         }
  20.     }
  21. }
  22.  
  23. var part1 = coordinatesX.Sum(CheckPositionX);
  24. var part2 = coordinatesA.Count(CheckPositionA);
  25.  
  26. Console.WriteLine($"Part 1: {part1}");
  27. Console.WriteLine($"Part 2: {part2}");
  28.  
  29. return;
  30.  
  31. int CheckPositionX(Position x)
  32. {
  33.     var result = 0;
  34.     for (var row = -1; row <= 1; row++)
  35.     {
  36.         for (var col = -1; col <= 1; col++)
  37.         {
  38.             if (row == 0 && col == 0)
  39.             {
  40.                 continue;
  41.             }
  42.  
  43.             var m = x.Move(row, col);
  44.             var a = m.Move(row, col);
  45.             var s = a.Move(row, col);
  46.  
  47.             if (m.OutOfBounds(gridSize) ||
  48.                 a.OutOfBounds(gridSize) ||
  49.                 s.OutOfBounds(gridSize))
  50.             {
  51.                 continue;
  52.             }
  53.  
  54.             if (input[m.Row][m.Col] == 'M' &&
  55.                 input[a.Row][a.Col] == 'A' &&
  56.                 input[s.Row][s.Col] == 'S')
  57.             {
  58.                 result++;
  59.             }
  60.         }
  61.     }
  62.  
  63.     return result;
  64. }
  65.  
  66. bool CheckPositionA(Position a)
  67. {
  68.     if (a.OutOfBounds(1, gridSize - 1))
  69.     {
  70.         return false;
  71.     }
  72.  
  73.     var topLeft = input[a.Row - 1][a.Col - 1];
  74.     var topRight = input[a.Row - 1][a.Col + 1];
  75.     var bottomLeft = input[a.Row + 1][a.Col - 1];
  76.     var bottomRight = input[a.Row + 1][a.Col + 1];
  77.  
  78.     return topLeft == 'M' && topRight == 'M' && bottomLeft == 'S' && bottomRight == 'S'
  79.         || topLeft == 'M' && topRight == 'S' && bottomLeft == 'M' && bottomRight == 'S'
  80.         || topLeft == 'S' && topRight == 'S' && bottomLeft == 'M' && bottomRight == 'M'
  81.         || topLeft == 'S' && topRight == 'M' && bottomLeft == 'S' && bottomRight == 'M';
  82. }
  83.  
  84. internal class Position(int row, int col)
  85. {
  86.     public readonly int Row = row;
  87.     public readonly int Col = col;
  88.  
  89.     public Position Move(int row, int col)
  90.     {
  91.         return new Position(Row + row, Col + col);
  92.     }
  93.  
  94.     public bool OutOfBounds(int size)
  95.     {
  96.         return OutOfBounds(0, size);
  97.     }
  98.  
  99.     public bool OutOfBounds(int start, int end)
  100.     {
  101.         return Row < start || Row >= end || Col < start || Col >= end;
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement