@@ -11,13 +11,13 @@ case class Pos(x: Int, y: Int):
11
11
def projX = Pos (x, 0 )
12
12
def projY = Pos (0 , y)
13
13
14
- val numericalKeypad = Map (
14
+ val numericKeypad = Map (
15
15
'7' -> Pos (0 , 0 ), '8' -> Pos (1 , 0 ), '9' -> Pos (2 , 0 ),
16
16
'4' -> Pos (0 , 1 ), '5' -> Pos (1 , 1 ), '6' -> Pos (2 , 1 ),
17
17
'1' -> Pos (0 , 2 ), '2' -> Pos (1 , 2 ), '3' -> Pos (2 , 2 ),
18
18
'0' -> Pos (1 , 3 ), 'A' -> Pos (2 , 3 ),
19
19
)
20
- val numericalKeypadPositions = numericalKeypad .values.toSet
20
+ val numericKeypadPositions = numericKeypad .values.toSet
21
21
22
22
val directionalKeypad = Map (
23
23
'^' -> Pos (1 , 0 ), 'A' -> Pos (2 , 0 ),
@@ -37,17 +37,17 @@ def minPathStep(from: Pos, to: Pos, positions: Set[Pos]): String =
37
37
val reverse = ! positions(from + shift.projX) || (positions(from + shift.projY) && shift.x > 0 )
38
38
if reverse then v + h + 'A' else h + v + 'A'
39
39
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
43
43
(s " A $input" ).map(keypad).sliding(2 ).map(p => minPathStep(p(0 ), p(1 ), positions)).mkString
44
44
45
45
def part1 (input : String ): Long =
46
46
input
47
47
.linesIterator
48
48
.filter(_.nonEmpty)
49
49
.map: line => // 029A
50
- val path1 = minPath(line, isNumerical = true ) // <A^A^^>AvvvA
50
+ val path1 = minPath(line, isNumeric = true ) // <A^A^^>AvvvA
51
51
val path2 = minPath(path1) // v<<A>>^A<A>A<AAv>A^A<vAAA^>A
52
52
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
53
53
val num = line.init.toLong // 29
@@ -66,7 +66,7 @@ def part1(input: String): Long =
66
66
val cache = collection.mutable.Map .empty[(Pos , Pos , Int , Int ), Long ]
67
67
def minPathStepCost (from : Pos , to : Pos , level : Int , maxLevel : Int ): Long =
68
68
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
70
70
val shift = to - from
71
71
val h = (if shift.x > 0 then " >" else " <" ) * shift.x.abs
72
72
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 =
76
76
})
77
77
78
78
def 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
80
80
(s " A $input" ).map(keypad).sliding(2 ).map(p => minPathStepCost(p(0 ), p(1 ), level, maxLevel)).sum
81
81
82
82
def part2 (input : String ): Long =
0 commit comments