Skip to content

Commit

Permalink
Solve 2024 day 23 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 23, 2024
1 parent 8340161 commit fe45dcf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/main/scala/eu/sim642/adventofcode2024/Day23.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,44 @@ object Day23 {

def count3CliquesT(edges: Set[Edge]): Int = find3Cliques(edges).count(_.exists(_.startsWith("t")))

// copied from 2018 day 23
// TODO: move to library
def maximumClique(neighbors: Map[Computer, Set[Computer]]): Set[Computer] = {
var best: Set[Computer] = Set.empty

def bronKerbosh(r: Set[Computer], p: Set[Computer], x: Set[Computer]): Unit = {
if (p.isEmpty && x.isEmpty) {
//println(r)
if (r.size > best.size)
best = r
}
else {
//val u = p.headOption.getOrElse(x.head)
val u = (p ++ x).maxBy(neighbors(_).size) // pivot on highest degree
var p2 = p
var x2 = x
for (v <- p -- neighbors(u)) {
bronKerbosh(r + v, p2 intersect neighbors(v), x2 intersect neighbors(v))
p2 -= v
x2 += v
}
}
}

bronKerbosh(Set.empty, neighbors.keySet, Set.empty)
best
}

def maximumClique(edges: Set[Edge]): Set[Computer] = {
val neighbors = (edges ++ edges.map(_.swap)).groupMap(_._1)(_._2)
maximumClique(neighbors)
}

def lanPartyPassword(edges: Set[Edge]): String = {
val clique = maximumClique(edges)
clique.toSeq.sorted.mkString(",")
}

def parseEdge(s: String): Edge = s match {
case s"$from-$to" => (from, to)
}
Expand All @@ -26,6 +64,7 @@ object Day23 {

def main(args: Array[String]): Unit = {
println(count3CliquesT(parseEdges(input)))
println(lanPartyPassword(parseEdges(input)))

// part 1: 2366 - too high (used contains 't' instead of startsWith 't')
}
Expand Down
8 changes: 8 additions & 0 deletions src/test/scala/eu/sim642/adventofcode2024/Day23Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ class Day23Test extends AnyFunSuite {
test("Part 1 input answer") {
assert(count3CliquesT(parseEdges(input)) == 1437)
}

test("Part 2 examples") {
assert(lanPartyPassword(parseEdges(exampleInput)) == "co,de,ka,ta")
}

test("Part 2 input answer") {
assert(lanPartyPassword(parseEdges(input)) == "da,do,gx,ly,mb,ns,nt,pz,sc,si,tp,ul,vl")
}
}

0 comments on commit fe45dcf

Please sign in to comment.