You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/2024/puzzles/day23.md
+176Lines changed: 176 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -2,10 +2,186 @@ import Solver from "../../../../../website/src/components/Solver.js"
2
2
3
3
# Day 23: LAN Party
4
4
5
+
by [@scarf005](https://github.com/scarf005)
6
+
5
7
## Puzzle description
6
8
7
9
https://adventofcode.com/2024/day/23
8
10
11
+
## Solution summary
12
+
13
+
The puzzle involves finding triangles and [maximal cliques](https://en.wikipedia.org/wiki/Clique_(graph_theory)). The task is to determine:
14
+
15
+
-**Part 1**: Find the number of triangles in the graph.
16
+
-**Part 2**: Find the size of the largest clique in the graph.
17
+
18
+
## Parsing the input
19
+
20
+
Both parts use undirected graphs, represented as:
21
+
22
+
```scala
23
+
typeConnection=Map[String, Set[String]]
24
+
25
+
defparse(input: String):Connection= input
26
+
.split('\n')
27
+
.toSet
28
+
.flatMap { cases"$a-$b"=>Set(a -> b, b -> a) } // 1)
29
+
.groupMap(_._1)(_._2) // 2)
30
+
```
31
+
32
+
-`1)`: both `a -> b` and `b -> a` are added to the graph so that the graph is undirected.
33
+
-`2)`: a fancier way to write `groupBy(_._1).mapValues(_.map(_._2))`, [check the docs](https://www.scala-lang.org/api/3.x/scala/collection/IterableOps.html#groupMap-fffff03a) for details.
34
+
35
+
## Part 1
36
+
37
+
The goal is to find triangles that have a computer whose name starts with `t`.
38
+
This could be checked by simply checking whether all three vertices are connected to each other, like:
defisValidTriangle(vertices: Set[String]):Boolean= vertices.toList match
63
+
caseList(a, b, c) => a <-> b && b <-> c && c <-> a
64
+
case _ =>false
65
+
66
+
connection
67
+
.flatMap { (vertex, neighbors) =>
68
+
neighbors
69
+
.subsets(2) // 1)
70
+
.map(_ + vertex) // 2)
71
+
.withFilter(_.exists(_.startsWith("t")))
72
+
.filter(isValidTriangle)
73
+
}
74
+
.toSet
75
+
.size
76
+
```
77
+
78
+
-`1)`: chooses two neighbors...
79
+
-`2)` ...and adds the vertex itself to form a triangle.
80
+
81
+
## Part 2
82
+
83
+
This part is more complex, but there's a generalization of the problem: [finding the size of the largest clique in the graph](https://en.wikipedia.org/wiki/Clique_(graph_theory)). We'll skip the explanation of the algorithm, but here's the code:
if (newClique.size > currentMax.size) newClique else currentMax
180
+
}
181
+
182
+
bronKerbosch(potential = connections.keySet)
183
+
```
184
+
9
185
## Solutions from the community
10
186
-[Solution](https://github.com/nikiforo/aoc24/blob/main/src/main/scala/io/github/nikiforo/aoc24/D23T2.scala) by [Artem Nikiforov](https://github.com/nikiforo)
11
187
-[Solution](https://github.com/merlinorg/aoc2024/blob/main/src/main/scala/Day23.scala) by [merlinorg](https://github.com/merlinorg)
0 commit comments