11package eu .sim642 .adventofcode2025
22
3- import eu .sim642 .adventofcodelib .graph .{BFS , GraphSearch , TargetNode , UnitNeighbors }
3+ import eu .sim642 .adventofcodelib .graph .{BFS , Dijkstra , GraphSearch , TargetNode , UnitNeighbors }
44
55object Day10 {
66
77 type Lights = Vector [Boolean ]
88 type Buttons = Seq [Seq [Int ]]
9- type Joltages = Seq [Int ]
9+ type Joltages = Vector [Int ]
1010
1111 case class Machine (lights : Lights , buttons : Buttons , joltages : Joltages )
1212
@@ -25,11 +25,27 @@ object Day10 {
2525
2626 def sumFewestPresses (machines : Seq [Machine ]): Int = machines.map(fewestPresses).sum
2727
28+ // TODO: optimize
29+ def fewestPresses2 (machine : Machine ): Int = {
30+ val graphSearch = new GraphSearch [Joltages ] with UnitNeighbors [Joltages ] with TargetNode [Joltages ] {
31+ override val startNode : Joltages = machine.joltages.map(_ => 0 )
32+
33+ override def unitNeighbors (joltages : Joltages ): IterableOnce [Joltages ] =
34+ machine.buttons.map(_.foldLeft(joltages)((acc, i) => acc.updated(i, acc(i) + 1 )))
35+
36+ override val targetNode : Joltages = machine.joltages
37+ }
38+
39+ BFS .search(graphSearch).target.get._2
40+ }
41+
42+ def sumFewestPresses2 (machines : Seq [Machine ]): Int = machines.map(fewestPresses2).sum
43+
2844 def parseMachine (s : String ): Machine = s match {
2945 case s " [ $lightsStr] $buttonsStr { $joltagesStr} " =>
3046 val lights = lightsStr.map(_ == '#' ).toVector
3147 val buttons = buttonsStr.split(" " ).map(_.tail.init.split(" ," ).map(_.toInt).toSeq).toSeq
32- val joltages = joltagesStr.split(" ," ).map(_.toInt).toSeq
48+ val joltages = joltagesStr.split(" ," ).map(_.toInt).toVector
3349 Machine (lights, buttons, joltages)
3450 }
3551
@@ -39,5 +55,6 @@ object Day10 {
3955
4056 def main (args : Array [String ]): Unit = {
4157 println(sumFewestPresses(parseMachines(input)))
58+ println(sumFewestPresses2(parseMachines(input)))
4259 }
4360}
0 commit comments