Skip to content

Commit

Permalink
Day 10 (Part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Dec 16, 2023
1 parent f27099d commit 39e3c93
Show file tree
Hide file tree
Showing 5 changed files with 337 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Day10_2/Day10_2.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<None Remove="input.txt" />
</ItemGroup>

<ItemGroup>
<Content Include="input.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Tools\Tools.csproj" />
</ItemGroup>

</Project>
166 changes: 166 additions & 0 deletions Day10_2/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using Tools;

var input = File.ReadAllLines("input.txt");

var width = input[0].Length;
var height = input.Length;

var map = new char[width, height];
var distance = new int[width, height];
var mainLoop = new bool[width, height];

var startingPosition = new Point();

for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
distance[x, y] = -1;
map[x, y] = input[y][x];
if (map[x, y] == 'S')
{
startingPosition = new Point(x, y);
}
}
}

var loopLength = 0;

var stack = new Stack<(Point previous, Point current, int steps)>();
stack.Push((startingPosition, startingPosition, 0));
while (stack.Count > 0)
{
var (previousPosition, currentPosition, steps) = stack.Pop();
if (map[currentPosition.X, currentPosition.Y] == 'S' &&
distance[currentPosition.X, currentPosition.Y] != -1)
{
loopLength = steps;
break;
}

distance[currentPosition.X, currentPosition.Y] = steps;
steps++;

foreach (var direction in Directions.WithoutDiagonals)
{
var nextPosition = currentPosition + direction;
if (nextPosition.X < 0 || nextPosition.X >= width || nextPosition.Y < 0 || nextPosition.Y >= height)
{
continue;
}

if (nextPosition == previousPosition)
{
continue;
}

if (map[nextPosition.X, nextPosition.Y] == '.')
{
continue;
}

if (map[nextPosition.X, nextPosition.Y] != 'S' && distance[nextPosition.X, nextPosition.Y] != -1)
{
continue;
}

var currentPipe = map[currentPosition.X, currentPosition.Y];
var nextPipe = map[nextPosition.X, nextPosition.Y];

bool isValid = false;
if (direction == new Point(1, 0))
{
var nextPipeIsValid = (nextPipe == '7' || nextPipe == 'J' || nextPipe == '-' || nextPipe == 'S');
var currentPipeIsValid = (currentPipe == 'L' || currentPipe == '-' || currentPipe == 'S' || currentPipe == 'F');
isValid = nextPipeIsValid && currentPipeIsValid;
}
else if (direction == new Point(-1, 0))
{
var nextPipeIsValid = (nextPipe == 'F' || nextPipe == 'L' || nextPipe == '-' || nextPipe == 'S');
var currentPipeIsValid = (currentPipe == 'J' || currentPipe == '7' || currentPipe == 'S' || currentPipe == '-');
isValid = nextPipeIsValid && currentPipeIsValid;
}
else if (direction == new Point(0, 1))
{
var nextPipeIsValid = (nextPipe == 'L' || nextPipe == 'J' || nextPipe == '|' || nextPipe == 'S');
var currentPipeIsValid = (currentPipe == 'F' || currentPipe == '|' || currentPipe == 'S' || currentPipe == '7');
isValid = nextPipeIsValid && currentPipeIsValid;
}
else if (direction == new Point(0, -1))
{
var nextPipeIsValid = (nextPipe == 'F' || nextPipe == '7' || nextPipe == '|' || nextPipe == 'S');
var currentPipeIsValid = (currentPipe == 'L' || currentPipe == 'J' || currentPipe == 'S' || currentPipe == '|');
isValid = nextPipeIsValid && currentPipeIsValid;
}

if (isValid)
{
stack.Push((currentPosition, nextPosition, steps));
}
}
}

var originalLoopLength = loopLength;
mainLoop[startingPosition.X, startingPosition.Y] = true;
var loopPosition = startingPosition;
while (loopLength > 0)
{
loopLength--;
foreach (var direction in Directions.WithoutDiagonals)
{
var nextPosition = loopPosition + direction;
if (nextPosition.X < 0 || nextPosition.X >= width || nextPosition.Y < 0 || nextPosition.Y >= height)
{
continue;
}

if (distance[nextPosition.X, nextPosition.Y] == loopLength)
{
mainLoop[nextPosition.X, nextPosition.Y] = true;
loopPosition = nextPosition;
break;
}
}
}

long counter = 0;
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
if (!mainLoop[x, y])
{
if (InMainLoop(x, y))
{
counter++;
Console.Write('I');
}
else
{
Console.Write('O');
}
}
else
{
Console.Write('X');
}
}
Console.WriteLine();
}

Console.WriteLine(counter);

bool InMainLoop(int x, int y)
{
var crossings = 0;
x--;
for (; x >= 0; x--)
{
if (mainLoop[x, y] && (map[x, y] == '|' || map[x, y] == 'J' || map[x, y] == 'L' ))
{
crossings++;
}
}

return crossings % 2 != 0;
}
7 changes: 7 additions & 0 deletions Day10_2/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"profiles": {
"Run": {
"commandName": "Project"
}
}
}
Loading

0 comments on commit 39e3c93

Please sign in to comment.