From fd8508aa6f1329b86ae7cfeb2a210a414cf9e615 Mon Sep 17 00:00:00 2001 From: Adam Rodger Date: Mon, 23 Dec 2024 11:08:07 +0000 Subject: [PATCH] day(23): Tidy up some --- src/AdventOfCode/Day23.cs | 64 +++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/src/AdventOfCode/Day23.cs b/src/AdventOfCode/Day23.cs index eee3c47..47c01c0 100644 --- a/src/AdventOfCode/Day23.cs +++ b/src/AdventOfCode/Day23.cs @@ -1,7 +1,5 @@ -using System; using System.Collections.Generic; using System.Collections.Immutable; -using System.Diagnostics; using System.Linq; using AdventOfCode.Utilities; @@ -14,36 +12,24 @@ public class Day23 { public int Part1(string[] input) { - SortedDictionary> graph = new(); - SortedSet<(string, string)> edges = []; - - foreach (string line in input) - { - string[] elements = line.Split('-'); - Debug.Assert(elements.Length == 2); - - graph.GetOrCreate(elements[0], () => new SortedSet()).Add(elements[1]); - graph.GetOrCreate(elements[1], () => new SortedSet()).Add(elements[0]); - - if (string.Compare(elements[0], elements[1], StringComparison.Ordinal) < 0) - { - edges.Add((elements[0], elements[1])); - } - else - { - edges.Add((elements[1], elements[0])); - } - } + SortedDictionary> graph = BuildGraph(input); int total = 0; + /* Look for triangles of interconnected nodes, i.e. + + node + / \ + / \ + left -- right + */ foreach (string node in graph.Keys) { foreach (string left in graph[node].Where(l => string.CompareOrdinal(node, l) < 0)) { foreach (string right in graph[left].Where(r => string.CompareOrdinal(left, r) < 0)) { - if (!edges.Contains((node, right))) + if (!graph[node].Contains(right)) { continue; } @@ -57,8 +43,6 @@ public int Part1(string[] input) } } - // 148884 high - // 2819 high, but works for sample... Did a Contains instead of StartsWith... reading fail return total; } @@ -69,10 +53,9 @@ public string Part2(string[] input) foreach (string line in input) { string[] elements = line.Split('-'); - Debug.Assert(elements.Length == 2); - graph.GetOrCreate(elements[0], () => new SortedSet()).Add(elements[1]); - graph.GetOrCreate(elements[1], () => new SortedSet()).Add(elements[0]); + graph.GetOrCreate(elements[0], () => new List()).Add(elements[1]); + graph.GetOrCreate(elements[1], () => new List()).Add(elements[0]); } var cliques = new List>(); @@ -84,6 +67,29 @@ public string Part2(string[] input) } /// + /// Build the graph of every node to all the reachable nodes from that point + /// + /// Input edge descriptions + /// Graph + private static SortedDictionary> BuildGraph(string[] input) + { + SortedDictionary> graph = new(); + + foreach (string line in input) + { + string[] elements = line.Split('-'); + + graph.GetOrCreate(elements[0], () => new List()).Add(elements[1]); + graph.GetOrCreate(elements[1], () => new List()).Add(elements[0]); + } + + return graph; + } + + /// + /// Sort the graph into 'cliques', which are clusters in which every node is connected + /// to every other node. + /// /// Had to do some Googling for this one... Never heard of it before /// /// Graph of node ID to the IDs of all connected nodes @@ -103,7 +109,7 @@ private static void BronKerbosch(IDictionary> graph, return; } - foreach (var candidate in candidates.ToArray()) + foreach (string candidate in candidates.ToArray()) { BronKerbosch(graph, clique.Add(candidate),