Skip to content

Commit

Permalink
solve second puzzle correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
rpgrca committed Dec 24, 2024
1 parent 6c3f6b0 commit 5a65c14
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
35 changes: 32 additions & 3 deletions 2024/Day23/Day23.Logic/LanParty.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

using System.Net.Sockets;

namespace Day23.Logic;

public class LanParty
Expand Down Expand Up @@ -32,7 +30,6 @@ public List<string> FindPartiesOf(int size)
var result = new List<string>();
foreach (var computer in _connections)
{
if (computer.Value.Count >= size)
{
foreach (var other in computer.Value)
{
Expand Down Expand Up @@ -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<List<string>> FindLargestClique()
{
var computers = _connections.Keys.ToList();
var allCliques = new List<List<string>>();
ApplyBronKerboschAlgorithm(new List<string>(), computers, new List<string>(), _connections, allCliques);
return allCliques;
}

private void ApplyBronKerboschAlgorithm(List<string> R, List<string> P, List<string> X, Dictionary<string, List<string>> graph, List<List<string>> allCliques)
{
if (! (P.Any() || X.Any()))
{
allCliques.Add(new List<string>(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);
}
}
}
4 changes: 2 additions & 2 deletions 2024/Day23/Day23.UnitTests/LanPartyMust.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 5a65c14

Please sign in to comment.