Skip to content

Commit 0cde15e

Browse files
committed
Solve 2025 day 11 part 2
1 parent ae461b3 commit 0cde15e

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ object Day11 {
66

77
type Device = String
88

9-
def countPaths(devices: Map[Device, Seq[Device]], from: Device = "you", to: Device = "out"): Int = { // TODO: Long?
10-
9+
def countPaths(devices: Map[Device, Seq[Device]], from: Device = "you", to: Device = "out"): Int = {
1110
val memo = mutable.Map.empty[Device, Int]
1211

1312
def helper(device: Device): Int = {
@@ -22,6 +21,23 @@ object Day11 {
2221
helper(from)
2322
}
2423

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+
}
37+
38+
helper(from)(vias)
39+
}
40+
2541
def parseDevice(s: String): (Device, Seq[Device]) = s match {
2642
case s"$key: $values" => key -> values.split(" ").toSeq
2743
}
@@ -32,5 +48,6 @@ object Day11 {
3248

3349
def main(args: Array[String]): Unit = {
3450
println(countPaths(parseDevices(input)))
51+
println(countPaths2(parseDevices(input)))
3552
}
3653
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,34 @@ class Day11Test extends AnyFunSuite {
1717
|hhh: ccc fff iii
1818
|iii: out""".stripMargin
1919

20+
val exampleInput2 =
21+
"""svr: aaa bbb
22+
|aaa: fft
23+
|fft: ccc
24+
|bbb: tty
25+
|tty: ccc
26+
|ccc: ddd eee
27+
|ddd: hub
28+
|hub: fff
29+
|eee: dac
30+
|dac: fff
31+
|fff: ggg hhh
32+
|ggg: out
33+
|hhh: out""".stripMargin
34+
2035
test("Part 1 examples") {
2136
assert(countPaths(parseDevices(exampleInput)) == 5)
2237
}
2338

2439
test("Part 1 input answer") {
2540
assert(countPaths(parseDevices(input)) == 643)
2641
}
42+
43+
test("Part 2 examples") {
44+
assert(countPaths2(parseDevices(exampleInput2)) == 2)
45+
}
46+
47+
test("Part 2 input answer") {
48+
assert(countPaths2(parseDevices(input)) == 417190406827152L)
49+
}
2750
}

0 commit comments

Comments
 (0)