Skip to content

Commit

Permalink
Merge pull request #24 from MartinZikmund/feature/aoc2024-day23
Browse files Browse the repository at this point in the history
AoC 2024 Day 23 Solutions
  • Loading branch information
MartinZikmund authored Dec 23, 2024
2 parents e08dcde + 6905537 commit a8294f1
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/AdventOfCode.Puzzles/2024/23/Part1/Part1.cs
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;
}
}
92 changes: 92 additions & 0 deletions src/AdventOfCode.Puzzles/2024/23/Part2/Part2.cs
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;
}
}
32 changes: 32 additions & 0 deletions src/AdventOfCode.Puzzles/2024/23/TestData.txt
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

0 comments on commit a8294f1

Please sign in to comment.