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
e8d43ee
commit f5feee9
Showing
6 changed files
with
1,339 additions
and
8 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,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> |
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,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(); | ||
} |
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.