@@ -7,38 +7,35 @@ import de.ronny_h.aoc.extensions.SimpleCharGrid
77import de.ronny_h.aoc.extensions.asList
88import de.ronny_h.aoc.extensions.memoize
99
10- fun main () = KeypadConundrum ().run (94426 , 0 )
10+ fun main () = KeypadConundrum ().run (94426 , 118392478819140 )
1111
12- class KeypadConundrum : AdventOfCode <Int >(2024 , 21 ) {
13- fun input (code : String , keypad : Keypad , depth : Int ): String {
12+ class KeypadConundrum : AdventOfCode <Long >(2024 , 21 ) {
13+ fun input (code : String , keypad : Keypad , depth : Int ): Long {
1414 data class Parameters (val code : String , val depth : Int )
1515
16- val keypads = buildList(depth) {
17- repeat(depth) { add(Keypad (directionalKeypadLayout)) }
18- } + keypad
19- lateinit var inputRec: (Parameters ) -> String
16+ val keypads = List (depth) { Keypad (directionalKeypadLayout) } + keypad
17+ lateinit var inputRec: (Parameters ) -> Long
2018
2119 inputRec = { p: Parameters ->
2220 if (p.depth == 0 ) {
23- p.code
21+ p.code.length.toLong()
2422 } else {
2523 p.code.map { char ->
26- keypads[p.depth].moveTo(char).map {
24+ keypads[p.depth].moveTo(char).minOf {
2725 inputRec(Parameters (it, p.depth - 1 ))
28- }.minBy { it.length }
29- }.joinToString( " " )
26+ }
27+ }.sum( )
3028 }
3129 }.memoize()
3230
3331 return inputRec(Parameters (code, depth))
3432 }
3533
36- override fun part1 (input : List <String >): Int = input.sumOf {
37- input(it, Keypad (numericKeypadLayout), 3 ).length * it.dropLast(1 ).toInt()
38- }
34+ override fun part1 (input : List <String >): Long = sumOfComplexities(input, 3 )
35+ override fun part2 (input : List <String >): Long = sumOfComplexities(input, 26 )
3936
40- override fun part2 (input : List <String >) : Int = input.sumOf {
41- input(it , Keypad (numericKeypadLayout), 26 ).length * it .dropLast(1 ).toInt ()
37+ private fun sumOfComplexities (input : List <String >, depth : Int ): Long = input.sumOf { code ->
38+ input(code , Keypad (numericKeypadLayout), depth) * code .dropLast(1 ).toLong ()
4239 }
4340}
4441
0 commit comments