diff --git a/2024/Day23/Day23.Logic/LanParty.cs b/2024/Day23/Day23.Logic/LanParty.cs index d8971ced..9644e1a8 100644 --- a/2024/Day23/Day23.Logic/LanParty.cs +++ b/2024/Day23/Day23.Logic/LanParty.cs @@ -1,6 +1,4 @@ -using System.Net.Sockets; - namespace Day23.Logic; public class LanParty @@ -32,7 +30,6 @@ public List FindPartiesOf(int size) var result = new List(); foreach (var computer in _connections) { - if (computer.Value.Count >= size) { foreach (var other in computer.Value) { @@ -70,4 +67,36 @@ public int FindNetworksWithComputerStartingWith(int size, char initialLetter) return result.Distinct().Count(); } + + public string CalculateLargestNetworkParty() => + string.Join(',', FindLargestClique().OrderByDescending(c => c.Count).First().Order()); + + public List> FindLargestClique() + { + var computers = _connections.Keys.ToList(); + var allCliques = new List>(); + ApplyBronKerboschAlgorithm(new List(), computers, new List(), _connections, allCliques); + return allCliques; + } + + private void ApplyBronKerboschAlgorithm(List R, List P, List X, Dictionary> graph, List> allCliques) + { + if (! (P.Any() || X.Any())) + { + allCliques.Add(new List(R)); + return; + } + + var pivot = P.Concat(X).First(); + var nonNeighbors = P.Except(graph[pivot]).ToList(); + + foreach (var node in nonNeighbors) + { + R.Add(node); + ApplyBronKerboschAlgorithm(R, P.Intersect(graph[node]).ToList(), X.Intersect(graph[node]).ToList(), graph, allCliques); + R.Remove(node); + P.Remove(node); + X.Add(node); + } + } } diff --git a/2024/Day23/Day23.UnitTests/LanPartyMust.cs b/2024/Day23/Day23.UnitTests/LanPartyMust.cs index 72c31386..dcb5e0de 100644 --- a/2024/Day23/Day23.UnitTests/LanPartyMust.cs +++ b/2024/Day23/Day23.UnitTests/LanPartyMust.cs @@ -46,7 +46,7 @@ public void SolveFirstSampleCorrectly() public void SolveFirstPuzzleCorrectly() { var sut = new LanParty(PUZZLE_INPUT); - var result = sut.FindNetworksWithComputerStartingWith(3, 't'); - Assert.Equal(1175, result); + var result = sut.CalculateLargestNetworkParty(); + Assert.Equal("bw,dr,du,ha,mm,ov,pj,qh,tz,uv,vq,wq,xw", result); } } \ No newline at end of file