Skip to content

Commit

Permalink
day(23): Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
adamrodger committed Dec 23, 2024
1 parent 1620385 commit 395d3e8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
56 changes: 50 additions & 6 deletions src/AdventOfCode/Day23.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using AdventOfCode.Utilities;
Expand Down Expand Up @@ -36,9 +37,7 @@ public int Part1(string[] input)

int total = 0;

string[] nodes = graph.Keys.Order().ToArray();

foreach (string node in nodes)
foreach (string node in graph.Keys)
{
foreach (string left in graph[node].Where(l => string.CompareOrdinal(node, l) < 0))
{
Expand All @@ -49,6 +48,7 @@ public int Part1(string[] input)
continue;
}

// found a triangle
if (node.StartsWith('t') || left.StartsWith('t') || right.StartsWith('t'))
{
total++;
Expand All @@ -62,14 +62,58 @@ public int Part1(string[] input)
return total;
}

public int Part2(string[] input)
public string Part2(string[] input)
{
SortedDictionary<string, ICollection<string>> graph = new();

foreach (string line in input)
{
throw new NotImplementedException("Part 2 not implemented");
string[] elements = line.Split('-');
Debug.Assert(elements.Length == 2);

graph.GetOrCreate(elements[0], () => new SortedSet<string>()).Add(elements[1]);
graph.GetOrCreate(elements[1], () => new SortedSet<string>()).Add(elements[0]);
}

return 0;
var cliques = new List<IImmutableSet<string>>();
BronKerbosch(graph, [], graph.Keys.ToImmutableSortedSet(), [], cliques);

IImmutableSet<string> biggest = cliques.MaxBy(c => c.Count);

return string.Join(',', biggest.Order());
}

/// <summary>
/// Had to do some Googling for this one... Never heard of it before
/// </summary>
/// <param name="graph">Graph of node ID to the IDs of all connected nodes</param>
/// <param name="clique">Current clique being considered</param>
/// <param name="candidates">Candidate nodes to be added to the clique</param>
/// <param name="visited">Nodes already visited</param>
/// <param name="cliques">All cliques found</param>
private static void BronKerbosch(IDictionary<string, ICollection<string>> graph,
IImmutableSet<string> clique,
IImmutableSet<string> candidates,
IImmutableSet<string> visited,
ICollection<IImmutableSet<string>> cliques)
{
if (candidates.Count == 0)
{
cliques.Add(clique);
return;
}

foreach (var candidate in candidates.ToArray())
{
BronKerbosch(graph,
clique.Add(candidate),
candidates.Intersect(graph[candidate]),
visited.Intersect(graph[candidate]),
cliques);

candidates = candidates.Remove(candidate);
visited = visited.Add(candidate);
}
}
}
}
4 changes: 2 additions & 2 deletions tests/AdventOfCode.Tests/Day23Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void Part1_RealInput_ProducesCorrectResponse()
[Fact]
public void Part2_SampleInput_ProducesCorrectResponse()
{
var expected = -1;
var expected = "co,de,ka,ta";

var result = solver.Part2(GetSampleInput());

Expand All @@ -94,7 +94,7 @@ public void Part2_SampleInput_ProducesCorrectResponse()
[Fact]
public void Part2_RealInput_ProducesCorrectResponse()
{
var expected = -1;
var expected = "av,ax,dg,di,dw,fa,ge,kh,ki,ot,qw,vz,yw";

var result = solver.Part2(GetRealInput());
output.WriteLine($"Day 23 - Part 2 - {result}");
Expand Down

0 comments on commit 395d3e8

Please sign in to comment.