From 1951975d1bf9b624639498aa2b29787e26a699ae Mon Sep 17 00:00:00 2001 From: Dimitrios Kaltzidis Date: Fri, 6 Dec 2024 17:40:20 +0200 Subject: [PATCH] Add part 2 --- Day6/Program.cs | 106 ++++++++++++++++++++++++------------ Day6/input.txt | 140 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 202 insertions(+), 44 deletions(-) diff --git a/Day6/Program.cs b/Day6/Program.cs index a97bc5f..c574619 100644 --- a/Day6/Program.cs +++ b/Day6/Program.cs @@ -25,25 +25,83 @@ for (var j = 0; j < charArray.GetLength(1); 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)) - { - guardDirection = (Direction)(((int)guardDirection + 1) % 4); - } - else - { - guardCurrentPosition = (nextPosition.Item1, nextPosition.Item2); - visitedPositions.Add([guardCurrentPosition.Item1, guardCurrentPosition.Item2]); - } + var potentialLoopPositions = new List<(int row, int col)>(); - 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()}"); -PrintMap(charArray, visitedPositions); +bool CausesLoop((int row, int col) startPosition, char[,] input) +{ + 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 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) { @@ -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] == '#'; -void PrintMap(char[,] input, List 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 { Up, diff --git a/Day6/input.txt b/Day6/input.txt index eccc2f3..f9dad84 100644 --- a/Day6/input.txt +++ b/Day6/input.txt @@ -1,10 +1,130 @@ -....#..... -.........# -.......... -..#....... -.......#.. -.......... -.#..^..... -........#. -#......... -......#... \ No newline at end of file +...............................#.........#............#...#.............................#....................#.....#........##.... +..##..##.#.#....................##....#..#.....#...............#..#.....#.....................#...#.............#...#...#......... +...................#............#..................................................#............................#..#.............. +............#...................................................#.........................#..#....#....................#.......... +.#...........#...............................#..#....................................#............#.#............#................ +.........................#..........................................#.................#...#.....#.#............................... +..#..................#......................................#......#........#.......#..........#.........#..#.........#........... +..........#..............#....................................#................................................#........#........# +..#...............................................................................................................#.#..#.......... +....#....................##...................#..#....#....#.........#.......#.....................................#.............. +........#...................#.......#.........................................#.........#........#....................#........... +................................................................#.......#......#...........#................#..................... +...........................................#.............#...........##.#................................#...................#.... +............................#......#........#...#................................................................................. +.......#.............#...............................#.............................................#.............#.........#...... +......................................................#.........................#....#................................#........... +...............#...........................................#..........#.........#..#..................#........................... +.....................................##...#................................#..#......................#.......#..........#......... +..............#.......#....................#.....................................................#.......#...................##..# +...............................................#.............#..#...#.....#..................................................#.... +.....................................#........#...........................................................#......#................ +........##............#...............................#....#...................................................................... +.....#............#...................................................................#.............................#............. +........#...............#......#..........#........#.....#........#..........#...............................................#..#. +..................#.............#........#......#.................................................#..........................#.... +.................................#............#...............................................................#..##............... +......................................#.#...........#........#.................................................................#.. +...........#...........#..............#..#.#...........#............................................#...#.....................#... +....##......................#..........#..............#.............#.....#............................#....................#..... +..................#...................#..........#............................................#...#.....................#......... +.....................#...........#.........................................................#...................................... +.#..............#....#..#.#...##.........#....#.............#..........#.......#...#.............................#.........#...#.. +..................#...............#.....................#..#.....................#........#....................................... +......#..#......#...#..................##.............#...................#.....#.....#................................#........#. +....................#.#................#....#..................................................#............#................#.... +...........................#..#.#......#........................#......#.......................................................... +..#.#...............................................................#.#........................#.....................#..#......... +...#..............................#......................................................#...#....................#.#............. +....................#.......#...........#.....#.......................#........#...........................##..................... +............#.........................................................#..............................................#.........#.. +..................#.#..............................#......#..................................#..........#......#..#............... +....##................#.#..................#.....#...........................................#.......................##........... +......#...........................#......#.......#..........#................#..#......#..........#..............#..#............. +........#.................#...#.........................................#..........#.........#...................................# +.#..........#....#.....#.............#...........#..................#.................#...#....................................... +...........................................................#......#...#...........................................##........#....# +............#.............................................................................#..........................#............ +.........#..............................................................................................#.........#.....#......#.. +.........#.......................................................................#........................#..#.....#.#............ +............................#.............#..........#..#...........#......#..............#..........................#............ +..#.#............#.........#.........#..................#.......................#............................................#.... +...#......................#......................................................#...........#..........................#......... +.#....#...............................................................................................................#........... +.#..#................#................................................................#....^.................#.............#...... +........................................................................................#.....................#................... +........##................#.............................#......................................................................... +.#.#.................#.....................#.....................#..........................#.................#...............#... +........#....................................................................................................#.................... +##......#.............................#.......#........................................................................#.....##... +..........#..........................................#.................#.....#............................................#....... +......................#........................#.......................................#............#............................. +.............................#.#.......................................................................................#.......... +...#.........#.....#........................#.................#...........#.....................#................................. +.........#...........................................................................#...........................#................ +......#...#............#..........#........................#...................#...........#................................#..... +.....#...#....#..........................................................#......................#.....................#..........# +#.............#....................#...............................#.......#.#.............#.....................#.............#.. +............................................................#.......#...................#.................................#......# +...#..#......................................#.........................................#..#.#.......##.......#............#..#.... +.....#.#...#...#.#................................................................................................................ +...............................................................................#........#...................#..#.........#....#... +......#.........#.............#........................................................................................#.##....... +.......................#...................................................................................................#...... +.........................................................#....................................#...................##.........#.... +.#..............#.#.#.............................#........................#........................#...................#.#....... +.........#.....#..........................................................................#.................#.................#... +.........#..............#...............................#.........#......#.#...................................................... +......................................................................................................#....#............#.......#. +.....#.................#.......#.....#..................................#...............................................#......... +...............................................................#.............#.................................................#.. +...#.....................................................................#...#.......#......#..................................... +.................................#...#.#............................................................#...#......................... +...........#..#.................................#....#...................#.............#.........#................................ +..................................................#..#...........#...........................#.................................... +.....#.....................................#......#......#...........................................................#............ +..............#..........................................#.........#.............#..#............................#................ +..........................................................................#..........#.......................#.................... +.......##................#................................................................................................#....... +.#.....#.....#..................#............#......#............................#.........#...................................... +..##....#..................................................................................................................#...... +.......#......................#..................#...#.....................................#............#..............#.......... +...............................#................#...#.................................##......................#................... +...............#..............................................................................................#.....##.......#.... +................................#..........#........................................#..#.......................................... +..................................................#.................................................................#............. +....................#....#.....................#.................................#.........#.....#......#......................#.. +................#.............#............#................#........................................................#..........#. +.#.......#......##...................#....#..............................................................#.................#...... +.........#.#............................................................................#........................#................ +...............#........##.....#.....................................#..#...........#......#...................................... +......#.............................................###..#.........#..............#...........................#.......#........... +.....................................#.........#...........................................................##..................... +.#....#.......................#.........#................................................................................#..#..... +....#..#.......................................................................................................##............#.... +..#...................................#....#......#....................#...............................#.......................... +...................#....##........................................##..........................................#................... +..........#.........#.............................................................#..#.........#...#............#.......#..#...... +....#...........#...##.........................#......................#.........#..#.............................................. +....................#.....#.....................................#...............................................................#. +.........#.......................#.................#...#....................................................#.#......#........#... +.................#.................................#..#..............#..........#...#.........#........#.##....##................. +.............#...........................#......................#.......................#......................................... +...............#.....#......#........#....#.......................................#.............................#................. +.............#.....##.........#...#............................................................................................... +.................................................................................#.............#.....#...#..#..................... +.................................#......#.#...#.......#......................................#......#..........................#.. +#..#....................................#..#...................................................................................... +...#......#.......................#..........................#...........#.#................#.#.......#........................... +#....#...............#..........................#....................#....#................................#............##........ +...........#........................#..........#......................................#...................................#.....#. +......#................................#................#.............................#.........#...#........#.................... +.............#................#...........#.#...........#.......................................#...........#.#................... +.....#......................#..........................#.................................#.............#............#....#........ +.....#.........#.#..............................................................................#...................##............ +......#.....#..#.............#...............#......#..#.#.................................#...............#...#.................. +....................#.......................#...................#........................#...................#..#.....##.......... +...............#...........#..#...........................................#........................#....#......................... +.....................................#...................#.#............#......................................#.................. +.....#......................#.#....................#.............................................#...........................#..#. +..........#........#................#.........#...........................................#....#..#...........#..####..........#.. \ No newline at end of file