Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fe45dcf

Browse files
committed
Solve 2024 day 23 part 2
1 parent 8340161 commit fe45dcf

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

‎src/main/scala/eu/sim642/adventofcode2024/Day23.scala

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,44 @@ object Day23 {
1616

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

19+
// copied from 2018 day 23
20+
// TODO: move to library
21+
def maximumClique(neighbors: Map[Computer, Set[Computer]]): Set[Computer] = {
22+
var best: Set[Computer] = Set.empty
23+
24+
def bronKerbosh(r: Set[Computer], p: Set[Computer], x: Set[Computer]): Unit = {
25+
if (p.isEmpty && x.isEmpty) {
26+
//println(r)
27+
if (r.size > best.size)
28+
best = r
29+
}
30+
else {
31+
//val u = p.headOption.getOrElse(x.head)
32+
val u = (p ++ x).maxBy(neighbors(_).size) // pivot on highest degree
33+
var p2 = p
34+
var x2 = x
35+
for (v <- p -- neighbors(u)) {
36+
bronKerbosh(r + v, p2 intersect neighbors(v), x2 intersect neighbors(v))
37+
p2 -= v
38+
x2 += v
39+
}
40+
}
41+
}
42+
43+
bronKerbosh(Set.empty, neighbors.keySet, Set.empty)
44+
best
45+
}
46+
47+
def maximumClique(edges: Set[Edge]): Set[Computer] = {
48+
val neighbors = (edges ++ edges.map(_.swap)).groupMap(_._1)(_._2)
49+
maximumClique(neighbors)
50+
}
51+
52+
def lanPartyPassword(edges: Set[Edge]): String = {
53+
val clique = maximumClique(edges)
54+
clique.toSeq.sorted.mkString(",")
55+
}
56+
1957
def parseEdge(s: String): Edge = s match {
2058
case s"$from-$to" => (from, to)
2159
}
@@ -26,6 +64,7 @@ object Day23 {
2664

2765
def main(args: Array[String]): Unit = {
2866
println(count3CliquesT(parseEdges(input)))
67+
println(lanPartyPassword(parseEdges(input)))
2968

3069
// part 1: 2366 - too high (used contains 't' instead of startsWith 't')
3170
}

‎src/test/scala/eu/sim642/adventofcode2024/Day23Test.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,12 @@ class Day23Test extends AnyFunSuite {
4646
test("Part 1 input answer") {
4747
assert(count3CliquesT(parseEdges(input)) == 1437)
4848
}
49+
50+
test("Part 2 examples") {
51+
assert(lanPartyPassword(parseEdges(exampleInput)) == "co,de,ka,ta")
52+
}
53+
54+
test("Part 2 input answer") {
55+
assert(lanPartyPassword(parseEdges(input)) == "da,do,gx,ly,mb,ns,nt,pz,sc,si,tp,ul,vl")
56+
}
4957
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /