|
| 1 | +package cz.glubo.adventofcode.y2024.day21 |
| 2 | + |
| 3 | +import cz.glubo.adventofcode.utils.Direction |
| 4 | +import cz.glubo.adventofcode.utils.Grid |
| 5 | +import cz.glubo.adventofcode.utils.IVec2 |
| 6 | +import cz.glubo.adventofcode.utils.input.Input |
| 7 | +import cz.glubo.adventofcode.y2024.day21.Tile.A |
| 8 | +import cz.glubo.adventofcode.y2024.day21.Tile.DOWN |
| 9 | +import cz.glubo.adventofcode.y2024.day21.Tile.LAVA |
| 10 | +import cz.glubo.adventofcode.y2024.day21.Tile.LEFT |
| 11 | +import cz.glubo.adventofcode.y2024.day21.Tile.N0 |
| 12 | +import cz.glubo.adventofcode.y2024.day21.Tile.N1 |
| 13 | +import cz.glubo.adventofcode.y2024.day21.Tile.N2 |
| 14 | +import cz.glubo.adventofcode.y2024.day21.Tile.N3 |
| 15 | +import cz.glubo.adventofcode.y2024.day21.Tile.N4 |
| 16 | +import cz.glubo.adventofcode.y2024.day21.Tile.N5 |
| 17 | +import cz.glubo.adventofcode.y2024.day21.Tile.N6 |
| 18 | +import cz.glubo.adventofcode.y2024.day21.Tile.N7 |
| 19 | +import cz.glubo.adventofcode.y2024.day21.Tile.N8 |
| 20 | +import cz.glubo.adventofcode.y2024.day21.Tile.N9 |
| 21 | +import cz.glubo.adventofcode.y2024.day21.Tile.RIGHT |
| 22 | +import cz.glubo.adventofcode.y2024.day21.Tile.UP |
| 23 | +import io.klogging.noCoLogger |
| 24 | +import kotlinx.coroutines.flow.toList |
| 25 | +import java.util.PriorityQueue |
| 26 | + |
| 27 | +val logger = noCoLogger({}.javaClass.toString()) |
| 28 | + |
| 29 | +enum class Tile( |
| 30 | + var char: Char, |
| 31 | +) { |
| 32 | + UP('^'), |
| 33 | + DOWN('v'), |
| 34 | + LEFT('<'), |
| 35 | + RIGHT('>'), |
| 36 | + A('A'), |
| 37 | + N0('0'), |
| 38 | + N1('1'), |
| 39 | + N2('2'), |
| 40 | + N3('3'), |
| 41 | + N4('4'), |
| 42 | + N5('5'), |
| 43 | + N6('6'), |
| 44 | + N7('7'), |
| 45 | + N8('8'), |
| 46 | + N9('9'), |
| 47 | + LAVA('#'), |
| 48 | + ; |
| 49 | + |
| 50 | + companion object { |
| 51 | + fun fromChar(c: Char) = Tile.entries.first { it.char == c } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +data class Path( |
| 56 | + val moves: List<Direction>, |
| 57 | + val sum: IVec2, |
| 58 | +) : Comparable<Path> { |
| 59 | + override fun compareTo(other: Path) = this.moves.size.compareTo(other.moves.size) |
| 60 | + |
| 61 | + fun add(direction: Direction) = |
| 62 | + Path( |
| 63 | + moves + direction, |
| 64 | + sum + direction.vector, |
| 65 | + ) |
| 66 | + |
| 67 | + fun asString() = |
| 68 | + moves.joinToString("") { |
| 69 | + it.toCommandChar().toString() |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +class Layer( |
| 74 | + val keyboard: Grid<Tile>, |
| 75 | + val lowerLayer: Layer?, |
| 76 | + val layerName: String = "layer", |
| 77 | +) { |
| 78 | + private val pathCache = mutableMapOf<Pair<IVec2, IVec2>, List<Path>>() |
| 79 | + private val movesCache = mutableMapOf<String, List<String>>() |
| 80 | + private val shortestCache = mutableMapOf<String, Long>() |
| 81 | + |
| 82 | + fun getShortestLength(input: String): Long = |
| 83 | + shortestCache.getOrPut(input) { |
| 84 | + input |
| 85 | + .split("A") |
| 86 | + .dropLast(1) |
| 87 | + .sumOf { innerMovelet -> |
| 88 | + val movelet = "${innerMovelet}A" |
| 89 | + logger.debug { "$layerName movelet: $movelet" } |
| 90 | + |
| 91 | + getMovesForInput(movelet).minOf { moves -> |
| 92 | + lowerLayer?.getShortestLength(moves) ?: moves.length.toLong() |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + fun getMovesForInput(input: String): List<String> = |
| 98 | + movesCache.getOrPut(input) { |
| 99 | + logger.debug { "$layerName hydrating: $input" } |
| 100 | + var position = getPosition(A) |
| 101 | + var outputs = listOf("") |
| 102 | + |
| 103 | + input.forEach { c -> |
| 104 | + var targetPos = |
| 105 | + getPosition( |
| 106 | + Tile.fromChar(c), |
| 107 | + ) |
| 108 | + |
| 109 | + val moves = shortestPaths(position, targetPos) |
| 110 | + outputs = |
| 111 | + outputs.flatMap { o -> |
| 112 | + moves |
| 113 | + .map { path -> |
| 114 | + o + path.asString() |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + outputs = outputs.map { it + "A" } |
| 119 | + position = targetPos |
| 120 | + } |
| 121 | + logger.debug { "$layerName hydrated $input: $outputs" } |
| 122 | + return@getOrPut outputs |
| 123 | + } |
| 124 | + |
| 125 | + private fun shortestPaths( |
| 126 | + position: IVec2, |
| 127 | + targetPos: IVec2, |
| 128 | + ): List<Path> = |
| 129 | + pathCache.getOrPut(position to targetPos) { |
| 130 | + val paths = PriorityQueue<Path>() |
| 131 | + |
| 132 | + paths.add(Path(listOf(), IVec2(0, 0))) |
| 133 | + |
| 134 | + val result = mutableListOf<Path>() |
| 135 | + |
| 136 | + fun shortestLengthSoFar() = (result.firstOrNull()?.moves?.size ?: Int.MAX_VALUE) |
| 137 | + do { |
| 138 | + val path = paths.poll() |
| 139 | + val headPos = position + path.sum |
| 140 | + val head = keyboard[headPos] |
| 141 | + when { |
| 142 | + path.moves.size > shortestLengthSoFar() -> Unit |
| 143 | + headPos == targetPos -> result.add(path) |
| 144 | + head == null -> Unit |
| 145 | + head == LAVA -> Unit |
| 146 | + else -> |
| 147 | + paths.addAll( |
| 148 | + Direction.entries.map { |
| 149 | + path.add(it) |
| 150 | + }, |
| 151 | + ) |
| 152 | + } |
| 153 | + } while (paths.isNotEmpty()) |
| 154 | + |
| 155 | + return@getOrPut result |
| 156 | + } |
| 157 | + |
| 158 | + private fun getPosition(t: Tile) = |
| 159 | + keyboard.allIVec2().first { |
| 160 | + keyboard[it] == t |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +private fun numericGrid(): Grid<Tile> { |
| 165 | + val grid = |
| 166 | + Grid( |
| 167 | + width = 3, |
| 168 | + height = 4, |
| 169 | + fields = |
| 170 | + buildList { |
| 171 | + repeat(3 * 4) { |
| 172 | + add(LAVA) |
| 173 | + } |
| 174 | + }.toMutableList(), |
| 175 | + ) |
| 176 | + grid[IVec2(0, 0)] = N7 |
| 177 | + grid[IVec2(1, 0)] = N8 |
| 178 | + grid[IVec2(2, 0)] = N9 |
| 179 | + grid[IVec2(0, 1)] = N4 |
| 180 | + grid[IVec2(1, 1)] = N5 |
| 181 | + grid[IVec2(2, 1)] = N6 |
| 182 | + grid[IVec2(0, 2)] = N1 |
| 183 | + grid[IVec2(1, 2)] = N2 |
| 184 | + grid[IVec2(2, 2)] = N3 |
| 185 | + grid[IVec2(0, 3)] = LAVA |
| 186 | + grid[IVec2(1, 3)] = N0 |
| 187 | + grid[IVec2(2, 3)] = A |
| 188 | + return grid |
| 189 | +} |
| 190 | + |
| 191 | +private fun arrowGrid(): Grid<Tile> { |
| 192 | + val grid = |
| 193 | + Grid<Tile>( |
| 194 | + width = 3, |
| 195 | + height = 2, |
| 196 | + fields = |
| 197 | + buildList { |
| 198 | + repeat(3 * 2) { |
| 199 | + add(LAVA) |
| 200 | + } |
| 201 | + }.toMutableList(), |
| 202 | + ) |
| 203 | + grid[IVec2(0, 0)] = LAVA |
| 204 | + grid[IVec2(1, 0)] = UP |
| 205 | + grid[IVec2(2, 0)] = A |
| 206 | + grid[IVec2(0, 1)] = LEFT |
| 207 | + grid[IVec2(1, 1)] = DOWN |
| 208 | + grid[IVec2(2, 1)] = RIGHT |
| 209 | + return grid |
| 210 | +} |
| 211 | + |
| 212 | +fun buildLayers(n: Int): Layer { |
| 213 | + var currentLayer: Layer? = null |
| 214 | + |
| 215 | + repeat(n) { i -> |
| 216 | + currentLayer = |
| 217 | + Layer( |
| 218 | + arrowGrid(), |
| 219 | + currentLayer, |
| 220 | + "arrows-$i", |
| 221 | + ) |
| 222 | + } |
| 223 | + |
| 224 | + return Layer(numericGrid(), currentLayer, "numeric") |
| 225 | +} |
| 226 | + |
| 227 | +suspend fun y2024day21part1(input: Input): Long { |
| 228 | + logger.info("year 2024 day 21 part 1") |
| 229 | + val layers = buildLayers(2) |
| 230 | + return input |
| 231 | + .lineFlow() |
| 232 | + .toList() |
| 233 | + .sumOf { line -> |
| 234 | + getIntValue(line) * layers.getShortestLength(line) |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +suspend fun y2024day21part2(input: Input): Long { |
| 239 | + logger.info("year 2024 day 21 part 2") |
| 240 | + val layers = buildLayers(25) |
| 241 | + return input |
| 242 | + .lineFlow() |
| 243 | + .toList() |
| 244 | + .sumOf { line -> |
| 245 | + getIntValue(line) * layers.getShortestLength(line) |
| 246 | + } |
| 247 | +} |
| 248 | + |
| 249 | +private fun getIntValue(line: String) = |
| 250 | + line |
| 251 | + .dropWhile { it == '0' } |
| 252 | + .removeSuffix("A") |
| 253 | + .toInt() |
0 commit comments