WIP Day 21 part 1 - test works

This commit is contained in:
Sven Weidauer 2025-01-04 17:52:13 +01:00
parent e69277a731
commit 89445f1df9

View file

@ -19,16 +19,31 @@ fun main() {
"<v>" "<v>"
) )
println(keySequence("029A", numberPad)) val tests = listOf(
println(keySequence(keySequence("029A", numberPad), robotPad)) "029A",
Integer.parseInt() "980A",
println("029A".toInt()) "179A",
"456A",
"379A",
)
val part1 = inputs.fold(0) { acc, s ->
val robot1 = keySequence(s, numberPad)
val robot2 = keySequence(robot1, robotPad)
val type = keySequence(robot2, robotPad)
val numeric = s.dropLast(1).toInt()
acc + type.length * numeric
}
println("Part 1: $part1")
} }
fun keySequence(code: String, pad: CharGrid, start: Char = 'A'): String { fun keySequence(code: String, pad: CharGrid, start: Char = 'A'): String {
var position = pad.find(start) ?: error("Start position not found") var position = pad.find(start) ?: error("Start position not found")
val gap = pad.find(' ') ?: error("Gap not found") val gap = pad.find(' ')
val result = StringBuilder() val result = StringBuilder()
for (current in code) { for (current in code) {
@ -37,7 +52,7 @@ fun keySequence(code: String, pad: CharGrid, start: Char = 'A'): String {
val dx = target.x - position.x val dx = target.x - position.x
val dy = target.y - position.y val dy = target.y - position.y
if (target.y < gap.y) { if (position.y == gap?.y) {
result.appendMovement(dy, '^', 'v') result.appendMovement(dy, '^', 'v')
result.appendMovement(dx, '<', '>') result.appendMovement(dx, '<', '>')
} else { } else {