Skip to content

Commit

Permalink
Day 17
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Jan 12, 2024
1 parent 91cf8ca commit 65fea59
Show file tree
Hide file tree
Showing 11 changed files with 366 additions and 0 deletions.
12 changes: 12 additions & 0 deletions AdventOfCode.sln
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day16_1", "Day16_1\Day16_1.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day16_2", "Day16_2\Day16_2.csproj", "{CEC9381F-43E1-42A3-8D87-5D44309B67A2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day17_1", "Day17_1\Day17_1.csproj", "{84BC1C44-6E4B-4860-A92E-8E31D6DFBB94}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day17_2", "Day17_2\Day17_2.csproj", "{BD5D483C-4BA9-4A28-9551-A4ACBE6F541C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -207,6 +211,14 @@ Global
{CEC9381F-43E1-42A3-8D87-5D44309B67A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CEC9381F-43E1-42A3-8D87-5D44309B67A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CEC9381F-43E1-42A3-8D87-5D44309B67A2}.Release|Any CPU.Build.0 = Release|Any CPU
{84BC1C44-6E4B-4860-A92E-8E31D6DFBB94}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{84BC1C44-6E4B-4860-A92E-8E31D6DFBB94}.Debug|Any CPU.Build.0 = Debug|Any CPU
{84BC1C44-6E4B-4860-A92E-8E31D6DFBB94}.Release|Any CPU.ActiveCfg = Release|Any CPU
{84BC1C44-6E4B-4860-A92E-8E31D6DFBB94}.Release|Any CPU.Build.0 = Release|Any CPU
{BD5D483C-4BA9-4A28-9551-A4ACBE6F541C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BD5D483C-4BA9-4A28-9551-A4ACBE6F541C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BD5D483C-4BA9-4A28-9551-A4ACBE6F541C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BD5D483C-4BA9-4A28-9551-A4ACBE6F541C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
24 changes: 24 additions & 0 deletions Day17_1/Day17_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>
70 changes: 70 additions & 0 deletions Day17_1/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Tools;

var input = File.ReadAllLines("input.txt");
var width = input[0].Length;
var height = input.Length;
var map = new int[width, height];

for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
map[x, y] = input[y][x] - '0';
}
}

var visited = new HashSet<Node>();
var minDistances = new Dictionary<Node, int>();
minDistances[new Node((0, 0), (0, 1), 0)] = 0;
minDistances[new Node((0, 0), (1, 0), 0)] = 0;
while (true)
{
var minNode = minDistances.OrderBy(x => x.Value).FirstOrDefault();
minDistances.Remove(minNode.Key);
visited.Add(minNode.Key);
var currentNode = minNode.Key;
var totalHeat = minNode.Value;
var direction = currentNode.Direction;
var position = currentNode.Position;
var directionSteps = currentNode.DirectionSteps;

if (currentNode.Position == (width - 1, height - 1))
{
Console.WriteLine(totalHeat);
break;
}

if (directionSteps < 3)
{
TryNextNode(direction, directionSteps + 1);
}

var leftTurn = (direction.Y, -direction.X);
TryNextNode(leftTurn, 1);
var rightTurn = (-direction.Y, direction.X);
TryNextNode(rightTurn, 1);

void TryNextNode(Point direction, int steps)
{
var nextPosition = new Point(position.X + direction.X, position.Y + direction.Y);
if (nextPosition.X < 0 || nextPosition.X >= width || nextPosition.Y < 0 || nextPosition.Y >= height)
{
return;
}

int nextHeat = totalHeat + map[nextPosition.X, nextPosition.Y];
var nextNode = new Node(nextPosition, direction, steps);

if (visited.Contains(nextNode))
{
return;
}

if (!minDistances.ContainsKey(nextNode) || minDistances[nextNode] > nextHeat)
{
minDistances[nextNode] = nextHeat;
}
}
}

record Node(Point Position, Point Direction, int DirectionSteps);
7 changes: 7 additions & 0 deletions Day17_1/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"profiles": {
"Run": {
"commandName": "Project"
}
}
}
5 changes: 5 additions & 0 deletions Day17_1/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
111111111111
999999999991
999999999991
999999999991
999999999991
Empty file added Day17_1/output.txt
Empty file.
24 changes: 24 additions & 0 deletions Day17_2/Day17_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>
76 changes: 76 additions & 0 deletions Day17_2/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Tools;

const int MinDirectionSteps = 4;
const int MaxDirectionSteps = 10;

var input = File.ReadAllLines("input.txt");
var width = input[0].Length;
var height = input.Length;
var map = new int[width, height];

for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
map[x, y] = input[y][x] - '0';
}
}

var visited = new HashSet<Node>();
var minDistances = new Dictionary<Node, int>();
minDistances[new Node((0, 0), (0, 1), 0)] = 0;
minDistances[new Node((0, 0), (1, 0), 0)] = 0;
while (true)
{
var minNode = minDistances.OrderBy(x => x.Value).FirstOrDefault();
minDistances.Remove(minNode.Key);
visited.Add(minNode.Key);
var currentNode = minNode.Key;
var totalHeat = minNode.Value;
var direction = currentNode.Direction;
var position = currentNode.Position;
var directionSteps = currentNode.DirectionSteps;

if (currentNode.Position == (width - 1, height - 1) && currentNode.DirectionSteps >= MinDirectionSteps)
{
Console.WriteLine(totalHeat);
break;
}

if (directionSteps < MaxDirectionSteps)
{
TryNextNode(direction, directionSteps + 1);
}

if (directionSteps >= MinDirectionSteps)
{
var leftTurn = (direction.Y, -direction.X);
TryNextNode(leftTurn, 1);
var rightTurn = (-direction.Y, direction.X);
TryNextNode(rightTurn, 1);
}

void TryNextNode(Point direction, int steps)
{
var nextPosition = new Point(position.X + direction.X, position.Y + direction.Y);
if (nextPosition.X < 0 || nextPosition.X >= width || nextPosition.Y < 0 || nextPosition.Y >= height)
{
return;
}

int nextHeat = totalHeat + map[nextPosition.X, nextPosition.Y];
var nextNode = new Node(nextPosition, direction, steps);

if (visited.Contains(nextNode))
{
return;
}

if (!minDistances.ContainsKey(nextNode) || minDistances[nextNode] > nextHeat)
{
minDistances[nextNode] = nextHeat;
}
}
}

record Node(Point Position, Point Direction, int DirectionSteps);
7 changes: 7 additions & 0 deletions Day17_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 65fea59

Please sign in to comment.