From b8b2c709867b236aad42f414def2ea92bb165bb4 Mon Sep 17 00:00:00 2001 From: encse Date: Mon, 23 Dec 2024 12:03:46 +0100 Subject: [PATCH] 2024/23 --- 2024/Day23/README.md | 2 +- 2024/Day23/Solution.cs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/2024/Day23/README.md b/2024/Day23/README.md index dbb20908..1bc05ba2 100644 --- a/2024/Day23/README.md +++ b/2024/Day23/README.md @@ -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. \ No newline at end of file +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. \ No newline at end of file diff --git a/2024/Day23/Solution.cs b/2024/Day23/Solution.cs index d0b60826..07a79f57 100644 --- a/2024/Day23/Solution.cs +++ b/2024/Day23/Solution.cs @@ -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) { @@ -26,10 +26,10 @@ public object PartTwo(string input) { return components.Single(); } - HashSet GetSeed(Graph g) => g.Keys.Where(k=>k.StartsWith("t")).ToHashSet(); + HashSet GetSeed(Graph g) => g.Keys.ToHashSet(); HashSet Grow(Graph g, HashSet 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)