File tree Expand file tree Collapse file tree 2 files changed +36
-32
lines changed
Expand file tree Collapse file tree 2 files changed +36
-32
lines changed Original file line number Diff line number Diff line change 11package eu .sim642 .adventofcode2025
22
33import eu .sim642 .adventofcodelib .pos .Pos3
4- import eu .sim642 .adventofcodelib .IteratorImplicits ._
4+ import eu .sim642 .adventofcodelib .IteratorImplicits .*
5+ import eu .sim642 .adventofcodelib .UnionFind
56
67import scala .annotation .tailrec
78
@@ -26,37 +27,6 @@ object Day8 {
2627 .map(_._1)
2728 }
2829
29- class UnionFind [A ](val reprs : Map [A , A ]) {
30- // TODO: optimize
31-
32- def this (items : Seq [A ]) = {
33- this (items.map(x => x -> x).toMap)
34- }
35-
36- @ tailrec
37- final def findRepr (x : A ): A = {
38- val repr = reprs(x)
39- if (x == repr)
40- repr
41- else
42- findRepr(repr)
43- }
44-
45- def sameRepr (x : A , y : A ): Boolean =
46- findRepr(x) == findRepr(y)
47-
48- def unioned (x : A , y : A ): UnionFind [A ] = {
49- val xRepr = findRepr(x)
50- val yRepr = findRepr(y)
51- new UnionFind (reprs + (yRepr -> xRepr))
52- }
53-
54- def groups (): Seq [Seq [A ]] =
55- reprs.keys.groupBy(findRepr).values.map(_.toSeq).toSeq
56-
57- override def toString : String = reprs.toString()
58- }
59-
6030 def multiplySizesAfter (junctionBoxes : Seq [Pos3 ], after : Int = 1000 , sizes : Int = 3 ): Int = {
6131 val closestPairs = closestPairsSeq(junctionBoxes)
6232
Original file line number Diff line number Diff line change 1+ package eu .sim642 .adventofcodelib
2+
3+ import scala .annotation .tailrec
4+
5+ class UnionFind [A ](val reprs : Map [A , A ]) {
6+ // TODO: optimize
7+
8+ def this (items : Seq [A ]) = {
9+ this (items.map(x => x -> x).toMap)
10+ }
11+
12+ @ tailrec
13+ final def findRepr (x : A ): A = {
14+ val repr = reprs(x)
15+ if (x == repr)
16+ repr
17+ else
18+ findRepr(repr)
19+ }
20+
21+ def sameRepr (x : A , y : A ): Boolean =
22+ findRepr(x) == findRepr(y)
23+
24+ def unioned (x : A , y : A ): UnionFind [A ] = {
25+ val xRepr = findRepr(x)
26+ val yRepr = findRepr(y)
27+ new UnionFind (reprs + (yRepr -> xRepr))
28+ }
29+
30+ def groups (): Seq [Seq [A ]] =
31+ reprs.keys.groupBy(findRepr).values.map(_.toSeq).toSeq
32+
33+ override def toString : String = reprs.toString()
34+ }
You can’t perform that action at this time.
0 commit comments