Add part 2

This commit is contained in:
Dimitrios Kaltzidis
2024-12-06 17:40:20 +02:00
parent daffd58ed0
commit 1951975d1b
2 changed files with 202 additions and 44 deletions

View File

@@ -25,25 +25,83 @@ for (var j = 0; j < charArray.GetLength(1); j++)
charArray[i, j] = lines[i][j]; charArray[i, j] = lines[i][j];
} }
var nextPosition = CalculateNextPosition(guardCurrentPosition.Item1, guardCurrentPosition.Item2, guardDirection); var part1 = Part1(guardCurrentPosition, guardDirection, charArray, visitedPositions);
var part2 = Part2(guardCurrentPosition, charArray);
Console.WriteLine($"Part1: {part1}\nPart2: {part2}");
while (IsValid(nextPosition.Item1, nextPosition.Item2, charArray)) int Part2((int,int) currentPosition, char[,] input)
{ {
if (IsBlocked(nextPosition.Item1, nextPosition.Item2, charArray)) var potentialLoopPositions = new List<(int row, int col)>();
{
guardDirection = (Direction)(((int)guardDirection + 1) % 4);
}
else
{
guardCurrentPosition = (nextPosition.Item1, nextPosition.Item2);
visitedPositions.Add([guardCurrentPosition.Item1, guardCurrentPosition.Item2]);
}
nextPosition = CalculateNextPosition(guardCurrentPosition.Item1, guardCurrentPosition.Item2, guardDirection); for (var i = 0; i < input.GetLength(0); i++)
for (var j = 0; j < input.GetLength(1); j++)
{
if ((i, j) == currentPosition || input[i, j] == '#') continue;
var modifiedGrid = (char[,])input.Clone();
modifiedGrid[i, j] = '#';
if (CausesLoop(currentPosition, modifiedGrid))
{
potentialLoopPositions.Add((i, j));
}
}
return potentialLoopPositions.Count;
} }
Console.WriteLine($"Part1: {visitedPositions.Select(x => $"r{x[0]},c{x[1]}").Distinct().Count()}"); bool CausesLoop((int row, int col) startPosition, char[,] input)
PrintMap(charArray, visitedPositions); {
var currentPosition = startPosition;
var currentDirection = Direction.Up;
var visited = new HashSet<((int row, int col), Direction)>();
visited.Add((currentPosition, currentDirection));
while (true)
{
var nextPosition = CalculateNextPosition(currentPosition.row, currentPosition.col, currentDirection);
if (!IsValid(nextPosition.Item1, nextPosition.Item2, input))
return false;
if (input[nextPosition.Item1, nextPosition.Item2] == '#')
{
currentDirection = (Direction)(((int)currentDirection + 1) % 4);
}
else
{
currentPosition = nextPosition;
}
if (!visited.Add((currentPosition, currentDirection)))
{
return true;
}
}
}
int Part1((int,int) currentPosition, Direction direction, char[,] input, List<int[]> traveledPositions)
{
var nextPosition = CalculateNextPosition(currentPosition.Item1, currentPosition.Item2, direction);
while (IsValid(nextPosition.Item1, nextPosition.Item2, input))
{
if (IsBlocked(nextPosition.Item1, nextPosition.Item2, input))
{
direction = (Direction)(((int)direction + 1) % 4);
}
else
{
currentPosition = (nextPosition.Item1, nextPosition.Item2);
traveledPositions.Add([currentPosition.Item1, currentPosition.Item2]);
}
nextPosition = CalculateNextPosition(currentPosition.Item1, currentPosition.Item2, direction);
}
return traveledPositions.Select(x => $"r{x[0]},c{x[1]}").Distinct().Count();
}
(int, int) CalculateNextPosition(int row, int col, Direction direction) (int, int) CalculateNextPosition(int row, int col, Direction direction)
{ {
@@ -55,26 +113,6 @@ bool IsValid(int row, int column, char[,] grid) => row >= 0 && row < grid.GetLen
bool IsBlocked(int row, int col, char[,] grid) => grid[row, col] == '#'; bool IsBlocked(int row, int col, char[,] grid) => grid[row, col] == '#';
void PrintMap(char[,] input, List<int[]> visitedPositions)
{
for (var i = 0; i < input.GetLength(0); i++)
{
for (var j = 0; j < input.GetLength(1); j++)
{
if (visitedPositions.Any(x => x[0] == i && x[1] == j) && input[i, j] != '^')
{
Console.Write("X");
}
else
{
Console.Write(input[i, j]);
}
}
Console.WriteLine();
}
}
enum Direction enum Direction
{ {
Up, Up,

View File

@@ -1,10 +1,130 @@
....#..... ...............................#.........#............#...#.............................#....................#.....#........##....
.........# ..##..##.#.#....................##....#..#.....#...............#..#.....#.....................#...#.............#...#...#.........
.......... ...................#............#..................................................#............................#..#..............
..#....... ............#...................................................#.........................#..#....#....................#..........
.......#.. .#...........#...............................#..#....................................#............#.#............#................
.......... .........................#..........................................#.................#...#.....#.#...............................
.#..^..... ..#..................#......................................#......#........#.......#..........#.........#..#.........#...........
........#. ..........#..............#....................................#................................................#........#........#
#......... ..#...............................................................................................................#.#..#..........
......#... ....#....................##...................#..#....#....#.........#.......#.....................................#..............
........#...................#.......#.........................................#.........#........#....................#...........
................................................................#.......#......#...........#................#.....................
...........................................#.............#...........##.#................................#...................#....
............................#......#........#...#.................................................................................
.......#.............#...............................#.............................................#.............#.........#......
......................................................#.........................#....#................................#...........
...............#...........................................#..........#.........#..#..................#...........................
.....................................##...#................................#..#......................#.......#..........#.........
..............#.......#....................#.....................................................#.......#...................##..#
...............................................#.............#..#...#.....#..................................................#....
.....................................#........#...........................................................#......#................
........##............#...............................#....#......................................................................
.....#............#...................................................................#.............................#.............
........#...............#......#..........#........#.....#........#..........#...............................................#..#.
..................#.............#........#......#.................................................#..........................#....
.................................#............#...............................................................#..##...............
......................................#.#...........#........#.................................................................#..
...........#...........#..............#..#.#...........#............................................#...#.....................#...
....##......................#..........#..............#.............#.....#............................#....................#.....
..................#...................#..........#............................................#...#.....................#.........
.....................#...........#.........................................................#......................................
.#..............#....#..#.#...##.........#....#.............#..........#.......#...#.............................#.........#...#..
..................#...............#.....................#..#.....................#........#.......................................
......#..#......#...#..................##.............#...................#.....#.....#................................#........#.
....................#.#................#....#..................................................#............#................#....
...........................#..#.#......#........................#......#..........................................................
..#.#...............................................................#.#........................#.....................#..#.........
...#..............................#......................................................#...#....................#.#.............
....................#.......#...........#.....#.......................#........#...........................##.....................
............#.........................................................#..............................................#.........#..
..................#.#..............................#......#..................................#..........#......#..#...............
....##................#.#..................#.....#...........................................#.......................##...........
......#...........................#......#.......#..........#................#..#......#..........#..............#..#.............
........#.................#...#.........................................#..........#.........#...................................#
.#..........#....#.....#.............#...........#..................#.................#...#.......................................
...........................................................#......#...#...........................................##........#....#
............#.............................................................................#..........................#............
.........#..............................................................................................#.........#.....#......#..
.........#.......................................................................#........................#..#.....#.#............
............................#.............#..........#..#...........#......#..............#..........................#............
..#.#............#.........#.........#..................#.......................#............................................#....
...#......................#......................................................#...........#..........................#.........
.#....#...............................................................................................................#...........
.#..#................#................................................................#....^.................#.............#......
........................................................................................#.....................#...................
........##................#.............................#.........................................................................
.#.#.................#.....................#.....................#..........................#.................#...............#...
........#....................................................................................................#....................
##......#.............................#.......#........................................................................#.....##...
..........#..........................................#.................#.....#............................................#.......
......................#........................#.......................................#............#.............................
.............................#.#.......................................................................................#..........
...#.........#.....#........................#.................#...........#.....................#.................................
.........#...........................................................................#...........................#................
......#...#............#..........#........................#...................#...........#................................#.....
.....#...#....#..........................................................#......................#.....................#..........#
#.............#....................#...............................#.......#.#.............#.....................#.............#..
............................................................#.......#...................#.................................#......#
...#..#......................................#.........................................#..#.#.......##.......#............#..#....
.....#.#...#...#.#................................................................................................................
...............................................................................#........#...................#..#.........#....#...
......#.........#.............#........................................................................................#.##.......
.......................#...................................................................................................#......
.........................................................#....................................#...................##.........#....
.#..............#.#.#.............................#........................#........................#...................#.#.......
.........#.....#..........................................................................#.................#.................#...
.........#..............#...............................#.........#......#.#......................................................
......................................................................................................#....#............#.......#.
.....#.................#.......#.....#..................................#...............................................#.........
...............................................................#.............#.................................................#..
...#.....................................................................#...#.......#......#.....................................
.................................#...#.#............................................................#...#.........................
...........#..#.................................#....#...................#.............#.........#................................
..................................................#..#...........#...........................#....................................
.....#.....................................#......#......#...........................................................#............
..............#..........................................#.........#.............#..#............................#................
..........................................................................#..........#.......................#....................
.......##................#................................................................................................#.......
.#.....#.....#..................#............#......#............................#.........#......................................
..##....#..................................................................................................................#......
.......#......................#..................#...#.....................................#............#..............#..........
...............................#................#...#.................................##......................#...................
...............#..............................................................................................#.....##.......#....
................................#..........#........................................#..#..........................................
..................................................#.................................................................#.............
....................#....#.....................#.................................#.........#.....#......#......................#..
................#.............#............#................#........................................................#..........#.
.#.......#......##...................#....#..............................................................#.................#......
.........#.#............................................................................#........................#................
...............#........##.....#.....................................#..#...........#......#......................................
......#.............................................###..#.........#..............#...........................#.......#...........
.....................................#.........#...........................................................##.....................
.#....#.......................#.........#................................................................................#..#.....
....#..#.......................................................................................................##............#....
..#...................................#....#......#....................#...............................#..........................
...................#....##........................................##..........................................#...................
..........#.........#.............................................................#..#.........#...#............#.......#..#......
....#...........#...##.........................#......................#.........#..#..............................................
....................#.....#.....................................#...............................................................#.
.........#.......................#.................#...#....................................................#.#......#........#...
.................#.................................#..#..............#..........#...#.........#........#.##....##.................
.............#...........................#......................#.......................#.........................................
...............#.....#......#........#....#.......................................#.............................#.................
.............#.....##.........#...#...............................................................................................
.................................................................................#.............#.....#...#..#.....................
.................................#......#.#...#.......#......................................#......#..........................#..
#..#....................................#..#......................................................................................
...#......#.......................#..........................#...........#.#................#.#.......#...........................
#....#...............#..........................#....................#....#................................#............##........
...........#........................#..........#......................................#...................................#.....#.
......#................................#................#.............................#.........#...#........#....................
.............#................#...........#.#...........#.......................................#...........#.#...................
.....#......................#..........................#.................................#.............#............#....#........
.....#.........#.#..............................................................................#...................##............
......#.....#..#.............#...............#......#..#.#.................................#...............#...#..................
....................#.......................#...................#........................#...................#..#.....##..........
...............#...........#..#...........................................#........................#....#.........................
.....................................#...................#.#............#......................................#..................
.....#......................#.#....................#.............................................#...........................#..#.
..........#........#................#.........#...........................................#....#..#...........#..####..........#..