Skip to content

Commit

Permalink
Add the basic structures for day 21.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulWoitaschek committed Dec 24, 2024
1 parent 3cce8c4 commit 84eeabd
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
100 changes: 100 additions & 0 deletions 2024/src/main/kotlin/aoc/year2024/Day21.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package aoc.year2024

import aoc.library.Direction
import aoc.library.Point
import aoc.library.Puzzle
import aoc.library.move
import java.util.PriorityQueue

object Day21 : Puzzle<Long, Long>(day = 21) {

private const val UP = '^'
private const val DOWN = 'v'
private const val LEFT = '<'
private const val RIGHT = '>'
private const val ACTIVATE = 'A'

/**
* +---+---+
* | ^ | A |
* +---+---+---+
* | < | v | > |
* +---+---+---+
*/
private val DIRECTIONAL_KEYPAD = mapOf(
Point(1, 0) to UP,
Point(2, 0) to ACTIVATE,
Point(0, 1) to LEFT,
Point(1, 1) to DOWN,
Point(2, 1) to RIGHT,
)

/**
* +---+---+---+
* | 7 | 8 | 9 |
* +---+---+---+
* | 4 | 5 | 6 |
* +---+---+---+
* | 1 | 2 | 3 |
* +---+---+---+
* | 0 | A |
* +---+---+
*/
private val NUMERICAL_KEYPAD = mapOf(
Point(0, 0) to '7',
Point(1, 0) to '8',
Point(2, 0) to '9',
Point(0, 1) to '4',
Point(1, 1) to '5',
Point(2, 1) to '6',
Point(0, 2) to '1',
Point(1, 2) to '2',
Point(2, 2) to '3',
Point(0, 2) to '1',
Point(1, 2) to '2',
Point(2, 2) to '3',
Point(1, 3) to '0',
Point(2, 3) to ACTIVATE,
)

override fun solvePart1(input: String): Long {
TODO()
}

private fun Map<Point, Char>.findChar(toFind: Char): Point = firstNotNullOf { (point, char) ->
if (char == toFind) point else null
}

private fun findPath(
pattern: Map<Point, Char>,
from: Char,
to: Char,
): String {
val fromPosition = pattern.findChar(from)
val toPosition = pattern.findChar(to)
val visited = mutableSetOf<Point>()

data class State(val position: Point, val steps: String)

val queue = PriorityQueue<State>(compareBy { it.steps.length })
queue.add(State(fromPosition, ""))
while (true) {
val candidate = queue.remove()
if (candidate.position == toPosition) {
return candidate.steps
}
if (!visited.add(candidate.position)) continue
Direction.entries
.forEach { direction ->
val point = candidate.position.move(direction)
if (point in pattern) {
queue += State(point, candidate.steps + direction.arrow)
}
}
}
}

override fun solvePart2(input: String): Long {
TODO()
}
}
42 changes: 42 additions & 0 deletions 2024/src/test/kotlin/aoc/year2024/Day21Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package aoc.year2024

import aoc.library.solvePart1
import aoc.library.solvePart2
import io.kotest.matchers.longs.shouldBeExactly
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test

class Day21Test {

@Test
@Disabled
fun part1TestInput() {
Day21.solvePart1(
"""
029A
980A
179A
456A
379A
""".trimIndent(),
) shouldBeExactly 42
}

@Test
@Disabled
fun part1() {
Day21.solvePart1() shouldBeExactly 42
}

@Test
@Disabled
fun part2TestInput() {
Day21.solvePart2() shouldBeExactly 42
}

@Test
@Disabled
fun part2() {
Day21.solvePart2() shouldBeExactly 42
}
}

0 comments on commit 84eeabd

Please sign in to comment.