Skip to content

Commit a2562d5

Browse files
committed
Solve 2025 day 10 part 2 naively
1 parent 46fe35d commit a2562d5

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package 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

55
object 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
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,12 @@ class Day10Test extends AnyFunSuite {
1717
test("Part 1 input answer") {
1818
assert(sumFewestPresses(parseMachines(input)) == 449)
1919
}
20+
21+
test("Part 2 examples") {
22+
assert(sumFewestPresses2(parseMachines(exampleInput)) == 33)
23+
}
24+
25+
test("Part 2 input answer") {
26+
//assert(sumFewestPresses2(parseMachines(input)) == 449)
27+
}
2028
}

0 commit comments

Comments
 (0)