generated from MartinZikmund/template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91cf8ca
commit 65fea59
Showing
11 changed files
with
366 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"profiles": { | ||
"Run": { | ||
"commandName": "Project" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
111111111111 | ||
999999999991 | ||
999999999991 | ||
999999999991 | ||
999999999991 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"profiles": { | ||
"Run": { | ||
"commandName": "Project" | ||
} | ||
} | ||
} |
Oops, something went wrong.