From 9c202dd73bfc04cab77dbb4a79d7b48608c29b8a Mon Sep 17 00:00:00 2001 From: Simmo Saan Date: Mon, 23 Dec 2024 16:49:32 +0200 Subject: [PATCH] Clean up 2024 day 23 --- .../eu/sim642/adventofcode2024/Day23.scala | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main/scala/eu/sim642/adventofcode2024/Day23.scala b/src/main/scala/eu/sim642/adventofcode2024/Day23.scala index e5435c8a..e9e1ec97 100644 --- a/src/main/scala/eu/sim642/adventofcode2024/Day23.scala +++ b/src/main/scala/eu/sim642/adventofcode2024/Day23.scala @@ -7,9 +7,11 @@ object Day23 { type Computer = String type Edge = (Computer, Computer) - def find3Cliques(edges: Set[Edge]): Set[Set[Computer]] = { - val neighbors = (edges ++ edges.map(_.swap)).groupMap(_._1)(_._2) + def edges2neighbors(edges: Set[Edge]): Map[Computer, Set[Computer]] = + (edges ++ edges.map(_.swap)).groupMap(_._1)(_._2) + def find3Cliques(edges: Set[Edge]): Set[Set[Computer]] = { + val neighbors = edges2neighbors(edges) for { (from, to) <- edges third <- neighbors(from) & neighbors(to) @@ -18,15 +20,11 @@ object Day23 { def count3CliquesT(edges: Set[Edge]): Int = find3Cliques(edges).count(_.exists(_.startsWith("t"))) - def maximumClique(edges: Set[Edge]): Set[Computer] = { - val neighbors = (edges ++ edges.map(_.swap)).groupMap(_._1)(_._2) - BronKerbosch.maximumClique(neighbors) - } + def maximumClique(edges: Set[Edge]): Set[Computer] = + BronKerbosch.maximumClique(edges2neighbors(edges)) - def lanPartyPassword(edges: Set[Edge]): String = { - val clique = maximumClique(edges) - clique.toSeq.sorted.mkString(",") - } + def lanPartyPassword(edges: Set[Edge]): String = + maximumClique(edges).toSeq.sorted.mkString(",") def parseEdge(s: String): Edge = s match { case s"$from-$to" => (from, to)