Skip to content

Commit 13555e1

Browse files
committed
Refactor 2025 day 11
1 parent 0cde15e commit 13555e1

File tree

2 files changed

+32
-32
lines changed

2 files changed

+32
-32
lines changed

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,36 @@ object Day11 {
66

77
type Device = String
88

9-
def countPaths(devices: Map[Device, Seq[Device]], from: Device = "you", to: Device = "out"): Int = {
10-
val memo = mutable.Map.empty[Device, Int]
11-
12-
def helper(device: Device): Int = {
13-
memo.getOrElseUpdate(device, {
14-
if (device == to)
15-
1
16-
else
17-
devices(device).map(helper).sum
18-
})
9+
trait Part {
10+
val from: Device
11+
val via: Set[Device]
12+
val to: Device = "out"
13+
14+
def countPaths(devices: Map[Device, Seq[Device]]): Long = {
15+
val memo = mutable.Map.empty[Device, Map[Set[Device], Long]]
16+
17+
def helper(device: Device): Map[Set[Device], Long] = {
18+
memo.getOrElseUpdate(device, {
19+
val deviceVia = via.intersect(Set(device))
20+
if (device == to)
21+
Map(deviceVia -> 1)
22+
else
23+
devices(device).flatMap(helper).groupMapReduce(_._1 ++ deviceVia)(_._2)(_ + _)
24+
})
25+
}
26+
27+
helper(from)(via)
1928
}
20-
21-
helper(from)
2229
}
2330

24-
def countPaths2(devices: Map[Device, Seq[Device]], from: Device = "svr", to: Device = "out"): Long = {
25-
val vias = Set("dac", "fft")
26-
27-
val memo = mutable.Map.empty[Device, Map[Set[Device], Long]]
28-
29-
def helper(device: Device): Map[Set[Device], Long] = {
30-
memo.getOrElseUpdate(device, {
31-
if (device == to)
32-
Map(Set.empty -> 1)
33-
else
34-
devices(device).flatMap(helper).groupMapReduce(_._1)(_._2)(_ + _).map((k, v) => (k.union(vias.intersect(Set(device)))) -> v)
35-
})
36-
}
31+
object Part1 extends Part {
32+
override val from: Device = "you"
33+
override val via: Set[Device] = Set.empty
34+
}
3735

38-
helper(from)(vias)
36+
object Part2 extends Part {
37+
override val from: Device = "svr"
38+
override val via: Set[Device] = Set("dac", "fft")
3939
}
4040

4141
def parseDevice(s: String): (Device, Seq[Device]) = s match {
@@ -47,7 +47,7 @@ object Day11 {
4747
lazy val input: String = scala.io.Source.fromInputStream(getClass.getResourceAsStream("day11.txt")).mkString.trim
4848

4949
def main(args: Array[String]): Unit = {
50-
println(countPaths(parseDevices(input)))
51-
println(countPaths2(parseDevices(input)))
50+
println(Part1.countPaths(parseDevices(input)))
51+
println(Part2.countPaths(parseDevices(input)))
5252
}
5353
}

src/test/scala/eu/sim642/adventofcode2025/Day11Test.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ class Day11Test extends AnyFunSuite {
3333
|hhh: out""".stripMargin
3434

3535
test("Part 1 examples") {
36-
assert(countPaths(parseDevices(exampleInput)) == 5)
36+
assert(Part1.countPaths(parseDevices(exampleInput)) == 5)
3737
}
3838

3939
test("Part 1 input answer") {
40-
assert(countPaths(parseDevices(input)) == 643)
40+
assert(Part1.countPaths(parseDevices(input)) == 643)
4141
}
4242

4343
test("Part 2 examples") {
44-
assert(countPaths2(parseDevices(exampleInput2)) == 2)
44+
assert(Part2.countPaths(parseDevices(exampleInput2)) == 2)
4545
}
4646

4747
test("Part 2 input answer") {
48-
assert(countPaths2(parseDevices(input)) == 417190406827152L)
48+
assert(Part2.countPaths(parseDevices(input)) == 417190406827152L)
4949
}
5050
}

0 commit comments

Comments
 (0)