Advertisement
Guest User

Day 10

a guest
Dec 9th, 2024
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.40 KB | Software | 0 0
  1.  
  2. fun main() {
  3.     val input = readInput("Day10")
  4.     val heightMap = input.map { it.map { c -> c.digitToInt() } }
  5.  
  6.     val trailheads = findTrailheads(heightMap)
  7.  
  8.     val totalScore = trailheads.sumOf { calculateScore(heightMap, it) }
  9.     println("Part 1: Sum of trailhead scores: $totalScore")
  10.  
  11.     val totalRating = trailheads.sumOf { calculateRating(heightMap, it) }
  12.     println("Part 2: Sum of trailhead ratings: $totalRating")
  13. }
  14.  
  15. fun findTrailheads(heightMap: List<List<Int>>): List<Pair<Int, Int>> {
  16.     val trailheads = mutableListOf<Pair<Int, Int>>()
  17.     for (y in heightMap.indices) {
  18.         for (x in heightMap[y].indices) {
  19.             if (heightMap[y][x] == 0) {
  20.                 trailheads.add(Pair(x, y))
  21.             }
  22.         }
  23.     }
  24.     return trailheads
  25. }
  26.  
  27. fun calculateScore(heightMap: List<List<Int>>, start: Pair<Int, Int>): Int {
  28.     val visited = mutableSetOf<Pair<Int, Int>>()
  29.     val queue = ArrayDeque<Pair<Int, Int>>()
  30.     queue.add(start)
  31.     var score = 0
  32.  
  33.     while (queue.isNotEmpty()) {
  34.         val (x, y) = queue.removeFirst()
  35.         if (Pair(x, y) in visited) continue
  36.         visited.add(Pair(x, y))
  37.  
  38.         if (heightMap[y][x] == 9) {
  39.             score++
  40.             continue
  41.         }
  42.  
  43.         // Explore adjacent positions with height + 1
  44.         listOf(Pair(x + 1, y), Pair(x - 1, y), Pair(x, y + 1), Pair(x, y - 1)).forEach { (nx, ny) ->
  45.             if (nx in heightMap[0].indices && ny in heightMap.indices && heightMap[ny][nx] == heightMap[y][x] + 1) {
  46.                 queue.add(Pair(nx, ny))
  47.             }
  48.         }
  49.     }
  50.  
  51.     return score
  52. }
  53.  
  54. fun calculateRating(heightMap: List<List<Int>>, start: Pair<Int, Int>): Int {
  55.     val paths = mutableSetOf<List<Pair<Int, Int>>>()
  56.     val queue = ArrayDeque<List<Pair<Int, Int>>>()
  57.     queue.add(listOf(start))
  58.  
  59.     while (queue.isNotEmpty()) {
  60.         val path = queue.removeFirst()
  61.         val (x, y) = path.last()
  62.  
  63.         if (heightMap[y][x] == 9) {
  64.             paths.add(path) // Add the complete path to the set
  65.             continue
  66.         }
  67.  
  68.         listOf(Pair(x + 1, y), Pair(x - 1, y), Pair(x, y + 1), Pair(x, y - 1)).forEach { (nx, ny) ->
  69.             if (nx in heightMap[0].indices && ny in heightMap.indices && heightMap[ny][nx] == heightMap[y][x] + 1 && Pair(nx, ny) !in path) {
  70.                 queue.add(path + Pair(nx, ny))
  71.             }
  72.         }
  73.     }
  74.  
  75.     return paths.size
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement