Advertisement
mgla

Advent of Code 2024 - Day 6

Dec 6th, 2024 (edited)
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.94 KB | None | 0 0
  1. var input = await File.ReadAllLinesAsync("input.txt");
  2.  
  3. var gridSize = input.Length;
  4. var grid = new char[gridSize, gridSize];
  5. var guard = new Position(0, 0);
  6. var start = new Position(0, 0);
  7. var visited1 = new HashSet<Position>();
  8. var guardDirection = Direction.Up;
  9.  
  10. for (var row = 0; row < gridSize; row++)
  11. {
  12.     for (var col = 0; col < gridSize; col++)
  13.     {
  14.         grid[row, col] = input[row][col];
  15.  
  16.         if (input[row][col] == '^')
  17.         {
  18.             start = new Position(row, col);
  19.             guard = start;
  20.             grid[row, col] = 'X';
  21.             visited1.Add(start);
  22.         }
  23.     }
  24. }
  25.  
  26. var part1 = 1;
  27. var next = guard.Move(guardDirection);
  28.  
  29. while (!next.OutOfBounds(gridSize))
  30. {
  31.     if (GetValue(next) == '#')
  32.     {
  33.         guardDirection = guardDirection.TurnRight();
  34.     }
  35.     else
  36.     {
  37.         guard = next;
  38.         if (visited1.Add(guard))
  39.         {
  40.             grid[next.Row, next.Col] = 'X';
  41.             part1++;
  42.         }
  43.     }
  44.     next = guard.Move(guardDirection);
  45. }
  46.  
  47. Console.WriteLine($"Part 1: {part1}");
  48.  
  49. var part2 = 0;
  50. var visited2 = new HashSet<(Position, Direction)>(); //We also store the direction for loop detection in Part 2.
  51.  
  52. foreach (var position in visited1)
  53. {
  54.     var currentValue = GetValue(position);
  55.     var row = position.Row;
  56.     var col = position.Col;
  57.  
  58.     guard = start;
  59.     guardDirection = Direction.Up;
  60.     grid[row, col] = '#';
  61.  
  62.     visited2.Clear();
  63.     visited2.Add((start, Direction.Up));
  64.  
  65.     while (!guard.OutOfBounds(gridSize))
  66.     {
  67.         next = guard.Move(guardDirection);
  68.  
  69.         if (GetValue(next) == '#')
  70.         {
  71.             guardDirection = guardDirection.TurnRight();
  72.         }
  73.         else
  74.         {
  75.             guard = next;
  76.             if (!visited2.Add((guard, guardDirection)))
  77.             {
  78.                 part2++;
  79.                 break;
  80.             }
  81.         }
  82.     }
  83.  
  84.     grid[row, col] = currentValue;
  85.  
  86. }
  87.  
  88. Console.WriteLine($"Part 2: {part2}");
  89.  
  90. return;
  91.  
  92. char GetValue(Position position)
  93. {
  94.     return position.OutOfBounds(gridSize) ? default : grid[position.Row, position.Col];
  95. }
  96.  
  97. internal record Direction(int Row, int Col)
  98. {
  99.     public Direction TurnLeft()
  100.     {
  101.         return new Direction(-Col, Row);
  102.     }
  103.  
  104.     public Direction TurnRight()
  105.     {
  106.         return new Direction(Col, -Row);
  107.     }
  108.  
  109.     public static readonly Direction Up = new(-1, 0);
  110.     public static readonly Direction Down = new(1, 0);
  111.     public static readonly Direction Left = new(0, -1);
  112.     public static readonly Direction Right = new(0, 1);
  113. }
  114.  
  115. internal record Position(int Row, int Col)
  116. {
  117.     public Position Move(Direction dir)
  118.     {
  119.         return new Position(Row + dir.Row, Col + dir.Col);
  120.     }
  121.  
  122.     public bool OutOfBounds(int size)
  123.     {
  124.         return OutOfBounds(0, size);
  125.     }
  126.  
  127.     public bool OutOfBounds(int start, int end)
  128.     {
  129.         return Row < start || Row >= end || Col < start || Col >= end;
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement