Skip to content

Commit 76ecf85

Browse files
committed
feat(2024): day 23
1 parent cea612a commit 76ecf85

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

2024/day23.scala

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package `2024`.day23
2+
3+
import prelude.*
4+
import scala.collection.parallel.CollectionConverters.*
5+
import scala.annotation.tailrec
6+
7+
type Connection = Map[String, Set[String]]
8+
9+
def parse(input: String): Connection = input
10+
.split('\n')
11+
.toSet
12+
.flatMap { case s"$a-$b" => Set(a -> b, b -> a) }
13+
.groupMap(_._1)(_._2)
14+
15+
def part1(input: String) =
16+
val connection = parse(input)
17+
18+
extension (a: String)
19+
inline infix def <->(b: String) =
20+
connection(a).contains(b) && connection(b).contains(a)
21+
22+
def isValidTriangle(vertices: Set[String]): Boolean = vertices.toList match
23+
case List(v1, v2, v3) => v1 <-> v2 && v2 <-> v3 && v3 <-> v1
24+
case _ => false
25+
26+
connection.par
27+
.flatMap { (vertex, neighbors) =>
28+
neighbors
29+
.subsets(2)
30+
.map(_ + vertex)
31+
.withFilter(_.exists(_.startsWith("t")))
32+
.filter(isValidTriangle)
33+
}
34+
.toSet
35+
.size
36+
37+
def part2(input: String) =
38+
val connection = parse(input)
39+
findMaximumCliqueBronKerbosch(connection).toList.sorted.mkString(",")
40+
41+
@main def main() =
42+
val input = fromFile(".cache/2024/23.txt").mkString
43+
println(part1(input))
44+
println(part2(input))
45+
46+
def findMaximumCliqueBronKerbosch(connections: Connection): Set[String] =
47+
def bronKerbosch(
48+
potential: Set[String],
49+
excluded: Set[String] = Set.empty,
50+
result: Set[String] = Set.empty,
51+
): Set[String] =
52+
if (potential.isEmpty && excluded.isEmpty) then result
53+
else
54+
// Choose pivot to minimize branching
55+
val pivot = (potential ++ excluded)
56+
.maxBy(vertex => potential.count(connections(vertex).contains))
57+
58+
val remaining = potential -- connections(pivot)
59+
60+
remaining.foldLeft(Set.empty[String]) { (currentMax, vertex) =>
61+
val neighbors = connections(vertex)
62+
val newClique = bronKerbosch(
63+
result = result + vertex,
64+
potential = potential & neighbors,
65+
excluded = excluded & neighbors,
66+
)
67+
if (newClique.size > currentMax.size) newClique else currentMax
68+
}
69+
70+
bronKerbosch(potential = connections.keySet)

2024/day23.test.scala

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package `2024`.day23
2+
3+
import munit.FunSuite
4+
import prelude.dedent
5+
6+
val example = """
7+
kh-tc
8+
qp-kh
9+
de-cg
10+
ka-co
11+
yn-aq
12+
qp-ub
13+
cg-tb
14+
vc-aq
15+
tb-ka
16+
wh-tc
17+
yn-cg
18+
kh-ub
19+
ta-co
20+
de-co
21+
tc-td
22+
tb-wq
23+
wh-td
24+
ta-ka
25+
td-qp
26+
aq-cg
27+
wq-ub
28+
ub-vc
29+
de-ta
30+
wq-aq
31+
wq-vc
32+
wh-yn
33+
ka-de
34+
kh-ta
35+
co-tc
36+
wh-qp
37+
tb-vc
38+
td-yn
39+
""".dedent
40+
41+
class Test extends FunSuite:
42+
test("example"):
43+
assertEquals(part1(example), 7)
44+
assertEquals(part2(example), "co,de,ka,ta")

0 commit comments

Comments
 (0)