Skip to content

Commit ca588df

Browse files
committed
Optimized via pair solution in 2025 day 11
1 parent f6db1ca commit ca588df

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/main/scala/eu/sim642/adventofcode2025/Day11.scala

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,26 @@ object Day11 {
6262
/**
6363
* Optimized version of [[ViaMapSolution]], which only keeps the largest key pair from the mapping.
6464
* Others are useless for passing all the vias.
65+
* Also, just the size of the visited vias set is tracked, because incompatible sets can't arise.
6566
*/
6667
object ViaPairSolution extends Solution {
6768

68-
case class ViaPair(via: Set[Device], count: Long) {
69+
case class ViaPair(via: Int, count: Long) {
6970
infix def combine(that: ViaPair): ViaPair = {
7071
if (via == that.via)
7172
ViaPair(via, count + that.count)
72-
else if (via subsetOf that.via)
73+
else if (via < that.via)
7374
that
74-
else if (that.via subsetOf via)
75+
else // that.via < via
7576
this
76-
else
77-
throw new IllegalArgumentException("incomparable via pairs") // doesn't happen on DAG
7877
}
7978

80-
def unionVia(thatVia: Set[Device]): ViaPair =
81-
copy(via = via union thatVia)
79+
def unionVia(thatVia: Int): ViaPair =
80+
copy(via = via + thatVia)
8281
}
8382

8483
object ViaPair {
85-
val empty = ViaPair(Set.empty, 0)
84+
val empty = ViaPair(0, 0)
8685
}
8786

8887
trait ViaPairPartSolution extends PartSolution {
@@ -91,7 +90,7 @@ object Day11 {
9190

9291
def helper(device: Device): ViaPair = {
9392
memo.getOrElseUpdate(device, {
94-
val deviceVia = via.intersect(Set(device))
93+
val deviceVia = if (via(device)) 1 else 0
9594
if (device == to)
9695
ViaPair(deviceVia, 1)
9796
else
@@ -100,7 +99,7 @@ object Day11 {
10099
}
101100

102101
val viaPair = helper(from)
103-
assert(viaPair.via == via)
102+
assert(viaPair.via == via.size)
104103
viaPair.count
105104
}
106105
}

0 commit comments

Comments
 (0)