Skip to content

Commit a8294f1

Browse files
Merge pull request #24 from MartinZikmund/feature/aoc2024-day23
AoC 2024 Day 23 Solutions
2 parents e08dcde + 6905537 commit a8294f1

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
namespace AdventOfCode.Puzzles._2024._23.Part1;
2+
3+
public partial class Part1 : IPuzzleSolution
4+
{
5+
private Dictionary<string, HashSet<string>> _graph = new();
6+
7+
private HashSet<string> _vertices = new();
8+
9+
public async Task<string> SolveAsync(StreamReader inputReader)
10+
{
11+
var edges = await inputReader.ReadAllLinesAsync();
12+
13+
foreach (var edge in edges)
14+
{
15+
var neighbors = edge.Split("-");
16+
foreach (var neighbor in neighbors)
17+
{
18+
if (!_graph.ContainsKey(neighbor))
19+
{
20+
_graph[neighbor] = new HashSet<string>();
21+
}
22+
23+
if (!_vertices.Contains(neighbor))
24+
{
25+
_vertices.Add(neighbor);
26+
}
27+
}
28+
29+
_graph[neighbors[0]].Add(neighbors[1]);
30+
_graph[neighbors[1]].Add(neighbors[0]);
31+
}
32+
33+
var triangles = GetTrianglesWithT();
34+
35+
return triangles.Count.ToString();
36+
}
37+
38+
private HashSet<string> GetTrianglesWithT()
39+
{
40+
var results = new HashSet<string>();
41+
foreach (var vertex in _vertices.Where(v => v.StartsWith("t")))
42+
{
43+
foreach (var secondVertex in _vertices)
44+
{
45+
foreach (var thirdVertex in _vertices)
46+
{
47+
if (_graph[vertex].Contains(secondVertex) &&
48+
_graph[secondVertex].Contains(thirdVertex) &&
49+
_graph[thirdVertex].Contains(vertex))
50+
{
51+
results.Add(string.Join(",", new[] { vertex, secondVertex, thirdVertex }.OrderBy(v => v)));
52+
}
53+
}
54+
}
55+
}
56+
57+
return results;
58+
}
59+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
namespace AdventOfCode.Puzzles._2024._23.Part2;
2+
3+
public partial class Part2 : IPuzzleSolution
4+
{
5+
private Dictionary<string, HashSet<string>> _graph = new();
6+
7+
private HashSet<string> _vertices = new();
8+
9+
public async Task<string> SolveAsync(StreamReader inputReader)
10+
{
11+
var edges = await inputReader.ReadAllLinesAsync();
12+
13+
foreach (var edge in edges)
14+
{
15+
var neighbors = edge.Split("-");
16+
foreach (var neighbor in neighbors)
17+
{
18+
if (!_graph.ContainsKey(neighbor))
19+
{
20+
_graph[neighbor] = new HashSet<string>();
21+
}
22+
23+
if (!_vertices.Contains(neighbor))
24+
{
25+
_vertices.Add(neighbor);
26+
}
27+
}
28+
29+
_graph[neighbors[0]].Add(neighbors[1]);
30+
_graph[neighbors[1]].Add(neighbors[0]);
31+
}
32+
33+
var triangles = GetTriangles();
34+
var largestSet = FindLargestSet(triangles);
35+
return largestSet;
36+
}
37+
38+
private string FindLargestSet(HashSet<string> triangles)
39+
{
40+
var sortedVertices = _vertices.OrderBy(v => v).ToArray();
41+
42+
var currentSets = triangles;
43+
44+
var newSets = currentSets;
45+
do
46+
{
47+
currentSets = newSets;
48+
newSets = new HashSet<string>();
49+
50+
foreach (var currentSet in currentSets)
51+
{
52+
var verticesInSet = currentSet.Split(",");
53+
foreach (var vertex in sortedVertices)
54+
{
55+
if (!currentSet.Contains(vertex))
56+
{
57+
if (verticesInSet.All(v => _graph[v].Contains(vertex)))
58+
{
59+
var newSet = string.Join(",", verticesInSet.Append(vertex).OrderBy(v => v));
60+
newSets.Add(newSet);
61+
}
62+
}
63+
}
64+
}
65+
} while (newSets.Count != 0);
66+
67+
var firstSet = currentSets.First();
68+
return firstSet;
69+
}
70+
71+
private HashSet<string> GetTriangles()
72+
{
73+
var results = new HashSet<string>();
74+
foreach (var vertex in _vertices)
75+
{
76+
foreach (var secondVertex in _vertices)
77+
{
78+
foreach (var thirdVertex in _vertices)
79+
{
80+
if (_graph[vertex].Contains(secondVertex) &&
81+
_graph[secondVertex].Contains(thirdVertex) &&
82+
_graph[thirdVertex].Contains(vertex))
83+
{
84+
results.Add(string.Join(",", new[] { vertex, secondVertex, thirdVertex }.OrderBy(v => v)));
85+
}
86+
}
87+
}
88+
}
89+
90+
return results;
91+
}
92+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
kh-tc
2+
qp-kh
3+
de-cg
4+
ka-co
5+
yn-aq
6+
qp-ub
7+
cg-tb
8+
vc-aq
9+
tb-ka
10+
wh-tc
11+
yn-cg
12+
kh-ub
13+
ta-co
14+
de-co
15+
tc-td
16+
tb-wq
17+
wh-td
18+
ta-ka
19+
td-qp
20+
aq-cg
21+
wq-ub
22+
ub-vc
23+
de-ta
24+
wq-aq
25+
wq-vc
26+
wh-yn
27+
ka-de
28+
kh-ta
29+
co-tc
30+
wh-qp
31+
tb-vc
32+
td-yn

0 commit comments

Comments
 (0)