|
| 1 | +package aockt.y2024 |
| 2 | + |
| 3 | +import aockt.util.parse |
| 4 | +import aockt.util.spacial.* |
| 5 | +import io.github.jadarma.aockt.core.Solution |
| 6 | + |
| 7 | +object Y2024D10 : Solution { |
| 8 | + |
| 9 | + /** A hiking trail topographical map. */ |
| 10 | + private class TopographicMap(private val data: Grid<Int>) : Grid<Int> by data { |
| 11 | + |
| 12 | + /** The locations of trailheads, or points with zero altitude. */ |
| 13 | + val trailHeads: Set<Point> = data.points().mapNotNull { (p, v) -> p.takeIf { v == 0 } }.toSet() |
| 14 | + |
| 15 | + /** |
| 16 | + * Calculates the score of this [trailHead], or how many peaks can be reached from here. |
| 17 | + * If the point is not a trailhead, the score is 0. |
| 18 | + */ |
| 19 | + fun scoreOf(trailHead: Point): Int = trailPathsFrom(trailHead, distinct = true).size |
| 20 | + |
| 21 | + /** |
| 22 | + * Calculates the rating of this [trailHead], or how many different routes you can take to reach a peak. |
| 23 | + * If the point is not a trailhead, the rating is 0. |
| 24 | + */ |
| 25 | + fun ratingOf(trailHead: Point): Int = trailPathsFrom(trailHead, distinct = false).size |
| 26 | + |
| 27 | + /** |
| 28 | + * Finds all the trail paths from the [start] point, and returns the location of their peaks. |
| 29 | + * If the point is not a trailhead, returns an empty list. |
| 30 | + * If [distinct], duplicate paths to the same peak are discarded. |
| 31 | + */ |
| 32 | + private fun trailPathsFrom(start: Point, distinct: Boolean): List<Point> = buildList { |
| 33 | + if (start !in trailHeads) return@buildList |
| 34 | + val area = Area(width, height) |
| 35 | + val visited = mutableSetOf<Point>() |
| 36 | + |
| 37 | + fun recurse(point: Point) { |
| 38 | + val altitude = data[point] |
| 39 | + |
| 40 | + if (altitude == 9) { |
| 41 | + if (!distinct || point !in visited) add(point) |
| 42 | + visited.add(point) |
| 43 | + } |
| 44 | + |
| 45 | + Direction.all |
| 46 | + .asSequence() |
| 47 | + .map(point::move) |
| 48 | + .filter { it in area } |
| 49 | + .filter { data[it] == altitude + 1 } |
| 50 | + .forEach(::recurse) |
| 51 | + } |
| 52 | + |
| 53 | + recurse(start) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** Parse the [input] and return the [TopographicMap] of the hiking region. */ |
| 58 | + private fun parseInput(input: String): TopographicMap = parse { |
| 59 | + val data = Grid(input) { it.digitToInt() } |
| 60 | + TopographicMap(data) |
| 61 | + } |
| 62 | + |
| 63 | + override fun partOne(input: String): Int = parseInput(input).run { trailHeads.sumOf(::scoreOf) } |
| 64 | + override fun partTwo(input: String): Int = parseInput(input).run { trailHeads.sumOf(::ratingOf) } |
| 65 | +} |
0 commit comments