Skip to content

Commit

Permalink
Day 25
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Jun 27, 2024
1 parent e8d43ee commit f5feee9
Show file tree
Hide file tree
Showing 6 changed files with 1,339 additions and 8 deletions.
22 changes: 14 additions & 8 deletions AdventOfCode.sln
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day24_1", "Day24_1\Day24_1.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day24_2", "Day24_2\Day24_2.csproj", "{9EFF6200-BB72-4969-9612-3182A1E20A43}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Day25_1", "Day25_1\Day25_1.csproj", "{76D9EFBA-0FFA-4463-9249-93D2F6A5BC33}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -114,14 +116,6 @@ Global
{7ED15525-D801-438E-9800-79C2251C53AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7ED15525-D801-438E-9800-79C2251C53AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7ED15525-D801-438E-9800-79C2251C53AE}.Release|Any CPU.Build.0 = Release|Any CPU
{9EFF6200-BB72-4969-9612-3182A1E20A43}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9EFF6200-BB72-4969-9612-3182A1E20A43}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9EFF6200-BB72-4969-9612-3182A1E20A43}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9EFF6200-BB72-4969-9612-3182A1E20A43}.Release|Any CPU.Build.0 = Release|Any CPU
{C2B32C5F-0BA2-48C2-9538-81524D7C3D71}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C2B32C5F-0BA2-48C2-9538-81524D7C3D71}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C2B32C5F-0BA2-48C2-9538-81524D7C3D71}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C2B32C5F-0BA2-48C2-9538-81524D7C3D71}.Release|Any CPU.Build.0 = Release|Any CPU
{D5968FD4-5504-4B08-A64F-1BC7DE3BE7EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D5968FD4-5504-4B08-A64F-1BC7DE3BE7EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D5968FD4-5504-4B08-A64F-1BC7DE3BE7EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -302,6 +296,18 @@ Global
{A092FFB8-D3D1-426A-87FC-CB317521F3F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A092FFB8-D3D1-426A-87FC-CB317521F3F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A092FFB8-D3D1-426A-87FC-CB317521F3F5}.Release|Any CPU.Build.0 = Release|Any CPU
{C2B32C5F-0BA2-48C2-9538-81524D7C3D71}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C2B32C5F-0BA2-48C2-9538-81524D7C3D71}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C2B32C5F-0BA2-48C2-9538-81524D7C3D71}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C2B32C5F-0BA2-48C2-9538-81524D7C3D71}.Release|Any CPU.Build.0 = Release|Any CPU
{9EFF6200-BB72-4969-9612-3182A1E20A43}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9EFF6200-BB72-4969-9612-3182A1E20A43}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9EFF6200-BB72-4969-9612-3182A1E20A43}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9EFF6200-BB72-4969-9612-3182A1E20A43}.Release|Any CPU.Build.0 = Release|Any CPU
{76D9EFBA-0FFA-4463-9249-93D2F6A5BC33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{76D9EFBA-0FFA-4463-9249-93D2F6A5BC33}.Debug|Any CPU.Build.0 = Debug|Any CPU
{76D9EFBA-0FFA-4463-9249-93D2F6A5BC33}.Release|Any CPU.ActiveCfg = Release|Any CPU
{76D9EFBA-0FFA-4463-9249-93D2F6A5BC33}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
28 changes: 28 additions & 0 deletions Day25_1/Day25_1.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<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>
<PackageReference Include="MathNet.Spatial" Version="0.6.0" />
</ItemGroup>

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

</Project>
127 changes: 127 additions & 0 deletions Day25_1/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
var input = File.ReadAllLines("input.txt");

Dictionary<string, Vertex> vertices = new();

foreach (var line in input)
{
var parts = line.Split(':');
var name = parts[0].Trim();
var connections = parts[1].Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList();

if (!vertices.ContainsKey(name))
{
vertices[name] = new Vertex(name);
}

foreach (var connection in connections)
{
if (!vertices.ContainsKey(connection))
{
vertices[connection] = new Vertex(connection);
}

vertices[name].Connections.Add(vertices[connection]);
vertices[connection].Connections.Add(vertices[name]);
}
}

var startVertex = vertices.Values.First();
for (int i = 0; i < 3; i++)
{
var (farthestVertex, path) = FindFarthest(startVertex);

for (int j = 1; j < path.Length; j++)
{
var from = path[j - 1];
var to = path[j];
from.Connections.Remove(to);
to.Connections.Remove(from);
}

startVertex = farthestVertex;
}

var componentRepresentative = startVertex;
var componentSize = CalculateComponentSize(componentRepresentative);

Console.WriteLine(componentSize * (vertices.Count - componentSize));

(Vertex farthest, Vertex[] path) FindFarthest(Vertex source)
{
var queue = new Queue<Vertex>();
queue.Enqueue(source);
var visited = new HashSet<Vertex>();
var distances = new Dictionary<Vertex, int>();
Vertex farthest = source;
Dictionary<Vertex, Vertex?> previous = new();
previous[source] = null;
distances[source] = 0;

while (queue.Count > 0)
{
var current = queue.Dequeue();

if (visited.Contains(current))
{
continue;
}

visited.Add(current);

foreach (var connection in current.Connections)
{
if (visited.Contains(connection))
{
continue;
}

distances[connection] = distances[current] + 1;
previous[connection] = current;
queue.Enqueue(connection);

if (distances[connection] > distances[farthest])
{
farthest = connection;
}
}
}

var path = new List<Vertex>();
var pathVertex = farthest;
while (pathVertex != null)
{
path.Add(pathVertex);
pathVertex = previous[pathVertex];
}

return (farthest, path.ToArray());
}

int CalculateComponentSize(Vertex componentRepresentative)
{
var visited = new HashSet<Vertex>();
var queue = new Queue<Vertex>();
queue.Enqueue(componentRepresentative);
visited.Add(componentRepresentative);
while (queue.Count > 0)
{
var current = queue.Dequeue();
foreach (var connection in current.Connections)
{
if (visited.Contains(connection))
{
continue;
}

visited.Add(connection);
queue.Enqueue(connection);
}
}

return visited.Count;
}

public record Vertex(string Name)
{
public List<Vertex> Connections = new();
}
7 changes: 7 additions & 0 deletions Day25_1/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 f5feee9

Please sign in to comment.