Add Day 6 part 1
This commit is contained in:
@@ -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
|
||||
|
||||
16
Day6/Day6.csproj
Normal file
16
Day6/Day6.csproj
Normal file
@@ -0,0 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="input.txt">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
84
Day6/Program.cs
Normal file
84
Day6/Program.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
var lines = File.ReadAllLines("input.txt");
|
||||
var charArray = new char[lines.Length, lines[0].Length];
|
||||
Dictionary<Direction, (int dx, int dy)> 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<int[]>();
|
||||
|
||||
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<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
|
||||
{
|
||||
Up,
|
||||
Right,
|
||||
Down,
|
||||
Left
|
||||
}
|
||||
10
Day6/input.txt
Normal file
10
Day6/input.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
....#.....
|
||||
.........#
|
||||
..........
|
||||
..#.......
|
||||
.......#..
|
||||
..........
|
||||
.#..^.....
|
||||
........#.
|
||||
#.........
|
||||
......#...
|
||||
Reference in New Issue
Block a user