Skip to content

Replace "numerical" by "numeric" #797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions 2024/src/day21.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ case class Pos(x: Int, y: Int):
def projX = Pos(x, 0)
def projY = Pos(0, y)

val numericalKeypad = Map(
val numericKeypad = Map(
'7' -> Pos(0, 0), '8' -> Pos(1, 0), '9' -> Pos(2, 0),
'4' -> Pos(0, 1), '5' -> Pos(1, 1), '6' -> Pos(2, 1),
'1' -> Pos(0, 2), '2' -> Pos(1, 2), '3' -> Pos(2, 2),
'0' -> Pos(1, 3), 'A' -> Pos(2, 3),
)
val numericalKeypadPositions = numericalKeypad.values.toSet
val numericKeypadPositions = numericKeypad.values.toSet

val directionalKeypad = Map(
'^' -> Pos(1, 0), 'A' -> Pos(2, 0),
Expand All @@ -37,17 +37,17 @@ def minPathStep(from: Pos, to: Pos, positions: Set[Pos]): String =
val reverse = !positions(from + shift.projX) || (positions(from + shift.projY) && shift.x > 0)
if reverse then v + h + 'A' else h + v + 'A'

def minPath(input: String, isNumerical: Boolean = false): String =
val keypad = if isNumerical then numericalKeypad else directionalKeypad
val positions = if isNumerical then numericalKeypadPositions else directionalKeypadPositions
def minPath(input: String, isNumeric: Boolean = false): String =
val keypad = if isNumeric then numericKeypad else directionalKeypad
val positions = if isNumeric then numericKeypadPositions else directionalKeypadPositions
(s"A$input").map(keypad).sliding(2).map(p => minPathStep(p(0), p(1), positions)).mkString

def part1(input: String): Long =
input
.linesIterator
.filter(_.nonEmpty)
.map: line => // 029A
val path1 = minPath(line, isNumerical = true) // <A^A^^>AvvvA
val path1 = minPath(line, isNumeric = true) // <A^A^^>AvvvA
val path2 = minPath(path1) // v<<A>>^A<A>A<AAv>A^A<vAAA^>A
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
val num = line.init.toLong // 29
Expand All @@ -66,7 +66,7 @@ def part1(input: String): Long =
val cache = collection.mutable.Map.empty[(Pos, Pos, Int, Int), Long]
def minPathStepCost(from: Pos, to: Pos, level: Int, maxLevel: Int): Long =
cache.getOrElseUpdate((from, to, level, maxLevel), {
val positions = if level == 0 then numericalKeypadPositions else directionalKeypadPositions
val positions = if level == 0 then numericKeypadPositions else directionalKeypadPositions
val shift = to - from
val h = (if shift.x > 0 then ">" else "<") * shift.x.abs
val v = (if shift.y > 0 then "v" else "^") * shift.y.abs
Expand All @@ -76,7 +76,7 @@ def minPathStepCost(from: Pos, to: Pos, level: Int, maxLevel: Int): Long =
})

def minPathCost(input: String, level: Int, maxLevel: Int): Long =
val keypad = if level == 0 then numericalKeypad else directionalKeypad
val keypad = if level == 0 then numericKeypad else directionalKeypad
(s"A$input").map(keypad).sliding(2).map(p => minPathStepCost(p(0), p(1), level, maxLevel)).sum

def part2(input: String): Long =
Expand Down