Skip to content

Commit ce1648d

Browse files
committed
Simplify KeypadConundrum
Don't construct the tree of Nodes. Return the button sequences directly, instead.
1 parent fb2225c commit ce1648d

File tree

2 files changed

+11
-31
lines changed

2 files changed

+11
-31
lines changed

src/main/kotlin/de/ronny_h/aoc/year24/day21/KeypadConundrum.kt

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,20 @@ import de.ronny_h.aoc.extensions.asList
99
fun main() = KeypadConundrum().run(94426, 0)
1010

1111
class KeypadConundrum : AdventOfCode<Int>(2024, 21) {
12-
13-
data class Node(val code: String, val children: List<Node>) {
14-
fun collectSequences(level: Int = 0): String {
15-
if (children.isEmpty()) {
16-
check(level % 2 == 0)
17-
return code
18-
}
19-
return if (level % 2 == 0) {
20-
// direct children = sequence
21-
children.joinToString("") { it.collectSequences(level + 1) }
22-
} else {
23-
// direct children = different options (branches in the tree)
24-
children
25-
.map { it.collectSequences(level + 1) }
26-
.minBy { it.length }
27-
}
28-
}
29-
}
30-
31-
fun input(code: String, keypad: Keypad, depth: Int): Node {
12+
fun input(code: String, keypad: Keypad, depth: Int): String {
3213
if (depth == 0) {
33-
return Node(code, emptyList())
14+
return code
3415
}
35-
return Node(code, code.map { char ->
36-
Node("$char", keypad.moveTo(char).map {
16+
17+
return code.map { char ->
18+
keypad.moveTo(char).map {
3719
input(it, Keypad(directionalKeypadLayout), depth - 1)
38-
})
39-
})
20+
}.minBy { it.length }
21+
}.joinToString("")
4022
}
4123

4224
override fun part1(input: List<String>): Int = input.sumOf {
43-
input(it, Keypad(numericKeypadLayout), 3).collectSequences().length * it.dropLast(1).toInt()
25+
input(it, Keypad(numericKeypadLayout), 3).length * it.dropLast(1).toInt()
4426
}
4527

4628
override fun part2(input: List<String>): Int {

src/test/kotlin/de/ronny_h/aoc/year24/day21/KeypadConundrumTest.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package de.ronny_h.aoc.year24.day21
22

33
import de.ronny_h.aoc.extensions.asList
4-
import de.ronny_h.aoc.year24.day21.KeypadConundrum.Node
54
import io.kotest.core.spec.style.StringSpec
65
import io.kotest.matchers.collections.shouldBeIn
76
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder
@@ -33,24 +32,23 @@ class KeypadConundrumTest : StringSpec({
3332
}
3433

3534
"input v<<A>>^A, last robot" {
36-
KeypadConundrum().input("v<<A>>^A", Keypad(directionalKeypadLayout), 1).collectSequences() shouldBeIn
35+
KeypadConundrum().input("v<<A>>^A", Keypad(directionalKeypadLayout), 1) shouldBeIn
3736
listOf(
3837
"<vA<AA>>^AvAA<^A>A",
3938
"v<A<AA>^>AvAA^<A>A",
4039
)
4140
}
4241

4342
"input 0, all robots" {
44-
KeypadConundrum().input("0", Keypad(numericKeypadLayout), 3).collectSequences() shouldBeIn
43+
KeypadConundrum().input("0", Keypad(numericKeypadLayout), 3) shouldBeIn
4544
listOf(
4645
"<vA<AA>>^AvAA<^A>A",
4746
"v<A<AA>^>AvAA^<A>A",
4847
)
4948
}
5049

5150
"input with depth 1" {
52-
KeypadConundrum().input("v", Keypad(directionalKeypadLayout), 1) shouldBe
53-
Node("v", listOf(Node("v", listOf(Node("v<A", emptyList()), Node("<vA", emptyList())))))
51+
KeypadConundrum().input("v", Keypad(directionalKeypadLayout), 1) shouldBe "v<A"
5452
}
5553

5654
"part 1: The sum of the complexities of the five codes" {

0 commit comments

Comments
 (0)