@@ -11,13 +11,13 @@ case class Pos(x: Int, y: Int):
1111 def projX = Pos (x, 0 )
1212 def projY = Pos (0 , y)
1313
14- val numericalKeypad = Map (
14+ val numericKeypad = Map (
1515 '7' -> Pos (0 , 0 ), '8' -> Pos (1 , 0 ), '9' -> Pos (2 , 0 ),
1616 '4' -> Pos (0 , 1 ), '5' -> Pos (1 , 1 ), '6' -> Pos (2 , 1 ),
1717 '1' -> Pos (0 , 2 ), '2' -> Pos (1 , 2 ), '3' -> Pos (2 , 2 ),
1818 '0' -> Pos (1 , 3 ), 'A' -> Pos (2 , 3 ),
1919)
20- val numericalKeypadPositions = numericalKeypad .values.toSet
20+ val numericKeypadPositions = numericKeypad .values.toSet
2121
2222val directionalKeypad = Map (
2323 '^' -> Pos (1 , 0 ), 'A' -> Pos (2 , 0 ),
@@ -37,17 +37,17 @@ def minPathStep(from: Pos, to: Pos, positions: Set[Pos]): String =
3737 val reverse = ! positions(from + shift.projX) || (positions(from + shift.projY) && shift.x > 0 )
3838 if reverse then v + h + 'A' else h + v + 'A'
3939
40- def minPath (input : String , isNumerical : Boolean = false ): String =
41- val keypad = if isNumerical then numericalKeypad else directionalKeypad
42- val positions = if isNumerical then numericalKeypadPositions else directionalKeypadPositions
40+ def minPath (input : String , isNumeric : Boolean = false ): String =
41+ val keypad = if isNumeric then numericKeypad else directionalKeypad
42+ val positions = if isNumeric then numericKeypadPositions else directionalKeypadPositions
4343 (s " A $input" ).map(keypad).sliding(2 ).map(p => minPathStep(p(0 ), p(1 ), positions)).mkString
4444
4545def part1 (input : String ): Long =
4646 input
4747 .linesIterator
4848 .filter(_.nonEmpty)
4949 .map: line => // 029A
50- val path1 = minPath(line, isNumerical = true ) // <A^A^^>AvvvA
50+ val path1 = minPath(line, isNumeric = true ) // <A^A^^>AvvvA
5151 val path2 = minPath(path1) // v<<A>>^A<A>A<AAv>A^A<vAAA^>A
5252 val path3 = minPath(path2) // <vA<AA>>^AvAA<^A>Av<<A>>^AvA^Av<<A>>^AA<vA>A^A<A>Av<<A>A^>AAA<Av>A^A
5353 val num = line.init.toLong // 29
@@ -66,7 +66,7 @@ def part1(input: String): Long =
6666val cache = collection.mutable.Map .empty[(Pos , Pos , Int , Int ), Long ]
6767def minPathStepCost (from : Pos , to : Pos , level : Int , maxLevel : Int ): Long =
6868 cache.getOrElseUpdate((from, to, level, maxLevel), {
69- val positions = if level == 0 then numericalKeypadPositions else directionalKeypadPositions
69+ val positions = if level == 0 then numericKeypadPositions else directionalKeypadPositions
7070 val shift = to - from
7171 val h = (if shift.x > 0 then " >" else " <" ) * shift.x.abs
7272 val v = (if shift.y > 0 then " v" else " ^" ) * shift.y.abs
@@ -76,7 +76,7 @@ def minPathStepCost(from: Pos, to: Pos, level: Int, maxLevel: Int): Long =
7676 })
7777
7878def minPathCost (input : String , level : Int , maxLevel : Int ): Long =
79- val keypad = if level == 0 then numericalKeypad else directionalKeypad
79+ val keypad = if level == 0 then numericKeypad else directionalKeypad
8080 (s " A $input" ).map(keypad).sliding(2 ).map(p => minPathStepCost(p(0 ), p(1 ), level, maxLevel)).sum
8181
8282def part2 (input : String ): Long =
0 commit comments