Skip to content

Commit

Permalink
Day 21
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Feb 11, 2024
1 parent f48d30d commit 654b274
Show file tree
Hide file tree
Showing 11 changed files with 508 additions and 0 deletions.
12 changes: 12 additions & 0 deletions AdventOfCode.sln
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day20_1", "Day20_1\Day20_1.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day20_2", "Day20_2\Day20_2.csproj", "{3CB2C32D-2326-4FAE-9D34-56DDC1C2A463}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day21_1", "Day21_1\Day21_1.csproj", "{A39616AE-A2EF-4B33-933C-2A2D6104F32E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day21_2", "Day21_2\Day21_2.csproj", "{AC399EF1-B2BE-4089-8F96-EC503D79CC77}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -255,6 +259,14 @@ Global
{3CB2C32D-2326-4FAE-9D34-56DDC1C2A463}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3CB2C32D-2326-4FAE-9D34-56DDC1C2A463}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3CB2C32D-2326-4FAE-9D34-56DDC1C2A463}.Release|Any CPU.Build.0 = Release|Any CPU
{A39616AE-A2EF-4B33-933C-2A2D6104F32E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A39616AE-A2EF-4B33-933C-2A2D6104F32E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A39616AE-A2EF-4B33-933C-2A2D6104F32E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A39616AE-A2EF-4B33-933C-2A2D6104F32E}.Release|Any CPU.Build.0 = Release|Any CPU
{AC399EF1-B2BE-4089-8F96-EC503D79CC77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AC399EF1-B2BE-4089-8F96-EC503D79CC77}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AC399EF1-B2BE-4089-8F96-EC503D79CC77}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AC399EF1-B2BE-4089-8F96-EC503D79CC77}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
24 changes: 24 additions & 0 deletions Day21_1/Day21_1.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>
67 changes: 67 additions & 0 deletions Day21_1/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Tools;

const char Empty = '.';
const char Occupied = '#';
const char Start = 'S';

var input = File.ReadAllLines("input.txt");
var width = input[0].Length;
var height = input.Length;
Point startPosition = default;
var map = new char[width, height];
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
map[x, y] = input[y][x];
if (input[y][x] == Start)
{
startPosition = new Point(x, y);
map[x, y] = Empty;
}
}
}

var result = CalculateReachable(startPosition, 1000);
Console.WriteLine(result);

long CalculateReachable(Point startPosition, int steps)
{
HashSet<Point> visited = new();
HashSet<Point> reachable = new();
Queue<(Point Position, int CurrentSteps)> queue = new();
queue.Enqueue((startPosition, 0));
visited.Add(startPosition);

while (queue.Count > 0)
{
var (position, currentSteps) = queue.Dequeue();

if (steps % 2 == currentSteps % 2)
{
reachable.Add(position);
}

if (currentSteps == steps)
{
continue;
}

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

if (map[newPosition.X, newPosition.Y] == Empty && !visited.Contains(newPosition))
{
visited.Add(newPosition);
queue.Enqueue((newPosition, currentSteps + 1));
}
}
}

return reachable.Count;
}
7 changes: 7 additions & 0 deletions Day21_1/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"profiles": {
"Run": {
"commandName": "Project"
}
}
}
131 changes: 131 additions & 0 deletions Day21_1/input.txt

Large diffs are not rendered by default.

Empty file added Day21_1/output.txt
Empty file.
24 changes: 24 additions & 0 deletions Day21_2/Day21_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>
105 changes: 105 additions & 0 deletions Day21_2/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using Tools;

const char Empty = '.';
const char Occupied = '#';
const char Start = 'S';

const long TotalSteps = 26501365;

var input = File.ReadAllLines("input.txt");
var mapSize = input[0].Length;
Point startPosition = default;
var map = new char[mapSize, mapSize];
for (var y = 0; y < mapSize; y++)
{
for (var x = 0; x < mapSize; x++)
{
map[x, y] = input[y][x];
if (input[y][x] == Start)
{
startPosition = new Point(x, y);
map[x, y] = Empty;
}
}
}

checked
{
var oddMapStepCount = CalculateReachable(startPosition, 10017);
var evenMapStepCount = CalculateReachable(startPosition, 10020);

long farthestMap = (TotalSteps - mapSize / 2) / (mapSize);
var evenFullMapCount = farthestMap * farthestMap;
var oddFullMapCount = (farthestMap - 1) * (farthestMap - 1);

var oddStepsFull = oddFullMapCount * oddMapStepCount;
var evenStepsFull = evenFullMapCount * evenMapStepCount;

int smallTriangleSteps = (int)(TotalSteps - ((mapSize / 2) + (farthestMap - 1) * mapSize + (mapSize / 2) + 2));
var smallTriangleTopRightReachable = CalculateReachable(new Point(0, mapSize - 1), smallTriangleSteps);
var smallTriangleTopLeftReachable = CalculateReachable(new Point(mapSize - 1, mapSize - 1), smallTriangleSteps);
var smallTriangleBottomRightReachable = CalculateReachable(new Point(0, 0), smallTriangleSteps);
var smallTriangleBottomLeftReachable = CalculateReachable(new Point(mapSize - 1, 0), smallTriangleSteps);

var smallTriangleStepsFull = (smallTriangleTopRightReachable + smallTriangleTopLeftReachable + smallTriangleBottomRightReachable + smallTriangleBottomLeftReachable) * farthestMap;

int largeTriangleSteps = (int)(TotalSteps - ((mapSize / 2) + (farthestMap - 2) * mapSize + (mapSize / 2) + 2));
var largeTriangleTopRightReachable = CalculateReachable(new Point(0, mapSize - 1), largeTriangleSteps);
var largeTriangleTopLeftReachable = CalculateReachable(new Point(mapSize - 1, mapSize - 1), largeTriangleSteps);
var largeTriangleBottomRightReachable = CalculateReachable(new Point(0, 0), largeTriangleSteps);
var largeTriangleBottomLeftReachable = CalculateReachable(new Point(mapSize - 1, 0), largeTriangleSteps);

var largeTriangleStepsFull = (largeTriangleTopRightReachable + largeTriangleTopLeftReachable + largeTriangleBottomRightReachable + largeTriangleBottomLeftReachable) * (farthestMap - 1);

var edgeSteps = (int)(TotalSteps - ((mapSize / 2) + (farthestMap - 1) * mapSize + 1));
var edgeTopReachable = CalculateReachable(new Point(mapSize / 2, mapSize - 1), edgeSteps);
var edgeLeftReachable = CalculateReachable(new Point(mapSize - 1, mapSize / 2), edgeSteps);
var edgeBottomReachable = CalculateReachable(new Point(mapSize / 2, 0), edgeSteps);
var edgeRightReachable = CalculateReachable(new Point(0, mapSize / 2), edgeSteps);

var edgeStepsFull = edgeTopReachable + edgeLeftReachable + edgeBottomReachable + edgeRightReachable;

var result = oddStepsFull + evenStepsFull + smallTriangleStepsFull + largeTriangleStepsFull + edgeStepsFull;
Console.WriteLine(result);
}

long CalculateReachable(Point startPosition, int steps)
{
HashSet<Point> visited = new();
HashSet<Point> reachable = new();
Queue<(Point Position, int CurrentSteps)> queue = new();
queue.Enqueue((startPosition, 0));
visited.Add(startPosition);

while (queue.Count > 0)
{
var (position, currentSteps) = queue.Dequeue();

if (steps % 2 == currentSteps % 2)
{
reachable.Add(position);
}

if (currentSteps == steps)
{
continue;
}

foreach (var direction in Directions.WithoutDiagonals)
{
var newPosition = position + direction;
if (newPosition.X < 0 || newPosition.X >= mapSize || newPosition.Y < 0 || newPosition.Y >= mapSize)
{
continue;
}

if (map[newPosition.X, newPosition.Y] == Empty && !visited.Contains(newPosition))
{
visited.Add(newPosition);
queue.Enqueue((newPosition, currentSteps + 1));
}
}
}

return reachable.Count;
}
7 changes: 7 additions & 0 deletions Day21_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 654b274

Please sign in to comment.