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.
Merge pull request #24 from MartinZikmund/feature/aoc2024-day23
AoC 2024 Day 23 Solutions
- Loading branch information
Showing
3 changed files
with
183 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
namespace AdventOfCode.Puzzles._2024._23.Part1; | ||
|
||
public partial class Part1 : IPuzzleSolution | ||
{ | ||
private Dictionary<string, HashSet<string>> _graph = new(); | ||
|
||
private HashSet<string> _vertices = new(); | ||
|
||
public async Task<string> SolveAsync(StreamReader inputReader) | ||
{ | ||
var edges = await inputReader.ReadAllLinesAsync(); | ||
|
||
foreach (var edge in edges) | ||
{ | ||
var neighbors = edge.Split("-"); | ||
foreach (var neighbor in neighbors) | ||
{ | ||
if (!_graph.ContainsKey(neighbor)) | ||
{ | ||
_graph[neighbor] = new HashSet<string>(); | ||
} | ||
|
||
if (!_vertices.Contains(neighbor)) | ||
{ | ||
_vertices.Add(neighbor); | ||
} | ||
} | ||
|
||
_graph[neighbors[0]].Add(neighbors[1]); | ||
_graph[neighbors[1]].Add(neighbors[0]); | ||
} | ||
|
||
var triangles = GetTrianglesWithT(); | ||
|
||
return triangles.Count.ToString(); | ||
} | ||
|
||
private HashSet<string> GetTrianglesWithT() | ||
{ | ||
var results = new HashSet<string>(); | ||
foreach (var vertex in _vertices.Where(v => v.StartsWith("t"))) | ||
{ | ||
foreach (var secondVertex in _vertices) | ||
{ | ||
foreach (var thirdVertex in _vertices) | ||
{ | ||
if (_graph[vertex].Contains(secondVertex) && | ||
_graph[secondVertex].Contains(thirdVertex) && | ||
_graph[thirdVertex].Contains(vertex)) | ||
{ | ||
results.Add(string.Join(",", new[] { vertex, secondVertex, thirdVertex }.OrderBy(v => v))); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return results; | ||
} | ||
} |
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,92 @@ | ||
namespace AdventOfCode.Puzzles._2024._23.Part2; | ||
|
||
public partial class Part2 : IPuzzleSolution | ||
{ | ||
private Dictionary<string, HashSet<string>> _graph = new(); | ||
|
||
private HashSet<string> _vertices = new(); | ||
|
||
public async Task<string> SolveAsync(StreamReader inputReader) | ||
{ | ||
var edges = await inputReader.ReadAllLinesAsync(); | ||
|
||
foreach (var edge in edges) | ||
{ | ||
var neighbors = edge.Split("-"); | ||
foreach (var neighbor in neighbors) | ||
{ | ||
if (!_graph.ContainsKey(neighbor)) | ||
{ | ||
_graph[neighbor] = new HashSet<string>(); | ||
} | ||
|
||
if (!_vertices.Contains(neighbor)) | ||
{ | ||
_vertices.Add(neighbor); | ||
} | ||
} | ||
|
||
_graph[neighbors[0]].Add(neighbors[1]); | ||
_graph[neighbors[1]].Add(neighbors[0]); | ||
} | ||
|
||
var triangles = GetTriangles(); | ||
var largestSet = FindLargestSet(triangles); | ||
return largestSet; | ||
} | ||
|
||
private string FindLargestSet(HashSet<string> triangles) | ||
{ | ||
var sortedVertices = _vertices.OrderBy(v => v).ToArray(); | ||
|
||
var currentSets = triangles; | ||
|
||
var newSets = currentSets; | ||
do | ||
{ | ||
currentSets = newSets; | ||
newSets = new HashSet<string>(); | ||
|
||
foreach (var currentSet in currentSets) | ||
{ | ||
var verticesInSet = currentSet.Split(","); | ||
foreach (var vertex in sortedVertices) | ||
{ | ||
if (!currentSet.Contains(vertex)) | ||
{ | ||
if (verticesInSet.All(v => _graph[v].Contains(vertex))) | ||
{ | ||
var newSet = string.Join(",", verticesInSet.Append(vertex).OrderBy(v => v)); | ||
newSets.Add(newSet); | ||
} | ||
} | ||
} | ||
} | ||
} while (newSets.Count != 0); | ||
|
||
var firstSet = currentSets.First(); | ||
return firstSet; | ||
} | ||
|
||
private HashSet<string> GetTriangles() | ||
{ | ||
var results = new HashSet<string>(); | ||
foreach (var vertex in _vertices) | ||
{ | ||
foreach (var secondVertex in _vertices) | ||
{ | ||
foreach (var thirdVertex in _vertices) | ||
{ | ||
if (_graph[vertex].Contains(secondVertex) && | ||
_graph[secondVertex].Contains(thirdVertex) && | ||
_graph[thirdVertex].Contains(vertex)) | ||
{ | ||
results.Add(string.Join(",", new[] { vertex, secondVertex, thirdVertex }.OrderBy(v => v))); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return results; | ||
} | ||
} |
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,32 @@ | ||
kh-tc | ||
qp-kh | ||
de-cg | ||
ka-co | ||
yn-aq | ||
qp-ub | ||
cg-tb | ||
vc-aq | ||
tb-ka | ||
wh-tc | ||
yn-cg | ||
kh-ub | ||
ta-co | ||
de-co | ||
tc-td | ||
tb-wq | ||
wh-td | ||
ta-ka | ||
td-qp | ||
aq-cg | ||
wq-ub | ||
ub-vc | ||
de-ta | ||
wq-aq | ||
wq-vc | ||
wh-yn | ||
ka-de | ||
kh-ta | ||
co-tc | ||
wh-qp | ||
tb-vc | ||
td-yn |