Advertisement
Guest User

Untitled

a guest
Dec 18th, 2023
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.21 KB | Source Code | 0 0
  1. class LavaLagoon(instructions: List<String>) {
  2.     private val trenches: List<Pair<LongRange, LongRange>>
  3.  
  4.     init {
  5.         val dugOut = mutableListOf<Pair<LongRange, LongRange>>()
  6.         var runningPos = 0L to 0L
  7.         instructions.forEach {
  8.             val instruction = it.split(" ")
  9.  
  10.             val length = instruction[2].substring(2, 7).toLong(radix = 16)
  11.             val nextPos = when (instruction[2][7]) {
  12.                 '0' -> runningPos.first to runningPos.second + length
  13.                 '1' -> runningPos.first + length to runningPos.second
  14.                 '2' -> runningPos.first to runningPos.second - length
  15.                 else -> runningPos.first - length to runningPos.second
  16.             }
  17.  
  18.             val yRange = minOf(runningPos.first, nextPos.first)..maxOf(runningPos.first, nextPos.first)
  19.             val xRange = minOf(runningPos.second, nextPos.second)..maxOf(runningPos.second, nextPos.second)
  20.             dugOut += yRange to xRange
  21.  
  22.             runningPos = nextPos
  23.         }
  24.         trenches = dugOut
  25.         System.out.println("mapping done")
  26.     }
  27.  
  28.     fun countHoles(): Long {
  29.         val yRange = trenches.map { it.first.first }.min()!! .. trenches.map { it.first.last }.max()!!
  30.         val xRange = trenches.map { it.second.first }.min()!! .. trenches.map { it.second.last }.max()!!
  31.         var lastLine = 0L
  32.         return yRange.map { y ->
  33.             var isHole = false
  34.             var count = 0L
  35.             var lastDirection = 0
  36.             if (trenches.any {
  37.                     it.first.first == y || it.first.first == y - 1 || it.first.first == y + 1 ||
  38.                             it.first.last == y || it.first.last == y - 1 || it.first.last == y + 1
  39.                 }) {
  40.                 val filteredTrenches = trenches.filter { it.first.contains(y) || it.first.contains(y-1) || it.first.contains(y+1) }
  41.                 println("$y/${yRange.last}")
  42.                 xRange.forEach { x ->
  43.                     val trenchAbove = filteredTrenches.any { it.first.contains(y - 1) && it.second.contains(x) }
  44.                     val isTrench = filteredTrenches.any { it.first.contains(y) && it.second.contains(x) }
  45.                     val trenchBelow = filteredTrenches.any { it.first.contains(y + 1) && it.second.contains(x) }
  46.                     if (isTrench || isHole) count++
  47.                     if (isTrench && trenchAbove && trenchBelow) {
  48.                         isHole = !isHole
  49.                     } else if (isTrench && (trenchAbove || trenchBelow)) {
  50.                         val direction = if (trenchAbove) 1 else 2
  51.                         when (lastDirection) {
  52.                             0 -> {
  53.                                 isHole = !isHole
  54.                                 lastDirection = direction
  55.                             }
  56.                             direction -> {
  57.                                 isHole = !isHole
  58.                                 lastDirection = 0
  59.                             }
  60.                             else -> lastDirection = 0
  61.                         }
  62.                     }
  63.                 }
  64.                 lastLine = count
  65.                 count
  66.             } else {
  67.                 lastLine
  68.             }
  69.         }.sum()
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement