diff --git a/AdventOfCode2024.sln b/AdventOfCode2024.sln index 2313914..d59e856 100644 --- a/AdventOfCode2024.sln +++ b/AdventOfCode2024.sln @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day4", "Day4\Day4.csproj", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day5", "Day5\Day5.csproj", "{F6C4FEFC-795A-45A5-A227-3DCA06749BAD}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day6", "Day6\Day6.csproj", "{6E7CF4A7-6667-4081-9E54-36452956EE8C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {F6C4FEFC-795A-45A5-A227-3DCA06749BAD}.Debug|Any CPU.Build.0 = Debug|Any CPU {F6C4FEFC-795A-45A5-A227-3DCA06749BAD}.Release|Any CPU.ActiveCfg = Release|Any CPU {F6C4FEFC-795A-45A5-A227-3DCA06749BAD}.Release|Any CPU.Build.0 = Release|Any CPU + {6E7CF4A7-6667-4081-9E54-36452956EE8C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6E7CF4A7-6667-4081-9E54-36452956EE8C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6E7CF4A7-6667-4081-9E54-36452956EE8C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6E7CF4A7-6667-4081-9E54-36452956EE8C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Day6/Day6.csproj b/Day6/Day6.csproj new file mode 100644 index 0000000..e0c4c2e --- /dev/null +++ b/Day6/Day6.csproj @@ -0,0 +1,16 @@ + + + + Exe + net8.0 + enable + enable + + + + + Always + + + + diff --git a/Day6/Program.cs b/Day6/Program.cs new file mode 100644 index 0000000..a97bc5f --- /dev/null +++ b/Day6/Program.cs @@ -0,0 +1,84 @@ +var lines = File.ReadAllLines("input.txt"); +var charArray = new char[lines.Length, lines[0].Length]; +Dictionary moves = new() +{ + { Direction.Up, (-1, 0) }, + { Direction.Right, (0, 1) }, + { Direction.Down, (1, 0) }, + { Direction.Left, (0, -1) } +}; + +var guardCurrentPosition = (0, 0); +var guardDirection = Direction.Up; +var visitedPositions = new List(); + +for (var i = 0; i < charArray.GetLength(0); i++) +for (var j = 0; j < charArray.GetLength(1); j++) +{ + if (lines[i][j] == '^') + { + guardCurrentPosition.Item1 = i; + guardCurrentPosition.Item2 = j; + visitedPositions.Add([guardCurrentPosition.Item1, guardCurrentPosition.Item2]); + } + + charArray[i, j] = lines[i][j]; +} + +var nextPosition = CalculateNextPosition(guardCurrentPosition.Item1, guardCurrentPosition.Item2, guardDirection); + +while (IsValid(nextPosition.Item1, nextPosition.Item2, charArray)) +{ + 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]); + } + + nextPosition = CalculateNextPosition(guardCurrentPosition.Item1, guardCurrentPosition.Item2, guardDirection); +} + +Console.WriteLine($"Part1: {visitedPositions.Select(x => $"r{x[0]},c{x[1]}").Distinct().Count()}"); +PrintMap(charArray, visitedPositions); + +(int, int) CalculateNextPosition(int row, int col, Direction direction) +{ + var (dx, dy) = moves[direction]; + return (row + dx, col + dy); +} + +bool IsValid(int row, int column, char[,] grid) => row >= 0 && row < grid.GetLength(0) && column >= 0 && column < grid.GetLength(1); + +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, + Right, + Down, + Left +} \ No newline at end of file diff --git a/Day6/input.txt b/Day6/input.txt new file mode 100644 index 0000000..eccc2f3 --- /dev/null +++ b/Day6/input.txt @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#... \ No newline at end of file