Skip to content

Commit

Permalink
2024/23
Browse files Browse the repository at this point in the history
  • Loading branch information
encse committed Dec 23, 2024
1 parent ff5cc4b commit b8b2c70
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion 2024/Day23/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ _Visit the website for the full story and [full puzzle](https://adventofcode.com

We tackled a graph algorithm problem today, where we had to find maximal cliques in an undirected graph. The literature provides [efficient algorithms](https://en.wikipedia.org/wiki/Bron%E2%80%93Kerbosch_algorithm) for this problem, but our graph is not too large, so we can use a straightforward "poor man's" strategy as well.

I started with the _seed_ components, that is single element components for each node that starts with '_t_'. Then, I proceeded to _grow_ these components by adding a single node to them in all possible ways. I can put this in a loop, to get components with size 2, 3, 4, etc. _Part 1_ asks for the number of components that have 3 nodes. _Part 2_ asks for the one that cannot be grown anymore.
I started with the _seed_ components, that is single element components for each node. Then, I proceeded to _grow_ these components by adding a single node to them in all possible ways. I can put this in a loop, to get components with size 2, 3, 4, etc. _Part 1_ asks for the number of components that have 3 nodes and have at least one node starting with 't'. _Part 2_ asks for the biggest component that cannot be grown anymore.
6 changes: 3 additions & 3 deletions 2024/Day23/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public object PartOne(string input) {
var components = GetSeed(g);
components = Grow(g, components);
components = Grow(g, components);
return components.Count;
return components.Count(c => Members(c).Any(m=>m.StartsWith("t")));
}

public object PartTwo(string input) {
Expand All @@ -26,10 +26,10 @@ public object PartTwo(string input) {
return components.Single();
}

HashSet<Component> GetSeed(Graph g) => g.Keys.Where(k=>k.StartsWith("t")).ToHashSet();
HashSet<Component> GetSeed(Graph g) => g.Keys.ToHashSet();

HashSet<Component> Grow(Graph g, HashSet<Component> components) => (
from c in components
from c in components.AsParallel()
let members = Members(c)
from neighbour in members.SelectMany(m => g[m]).Distinct()
where !members.Contains(neighbour)
Expand Down

0 comments on commit b8b2c70

Please sign in to comment.