Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2023
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.17 KB | Source Code | 0 0
  1. class BrickStack(snapShot: List<String>) {
  2.     val brickStack: List<Brick>
  3.  
  4.     init {
  5.         val fallingBricks = snapShot.map {
  6.             val brickDescription = it.split("~").map { coords -> coords.split(",").map(String::toInt) }
  7.             Brick(
  8.                 brickDescription[0][0]..brickDescription[1][0],
  9.                 brickDescription[0][1]..brickDescription[1][1],
  10.                 brickDescription[0][2]..brickDescription[1][2]
  11.             )
  12.         }.sortedBy { brick -> brick.z.first }
  13.         val fallenBricks = mutableListOf<Brick>()
  14.         fallingBricks.forEach { brick ->
  15.             var fallingBrick = brick
  16.             var supported = false
  17.             while (!supported) {
  18.                 val supportingBricks = fallenBricks.filter {
  19.                     it.x.intersect(fallingBrick.x).isNotEmpty() && it.y.intersect(fallingBrick.y).isNotEmpty() && it.z.last == fallingBrick.z.first - 1
  20.                 }
  21.                 supported = supportingBricks.isNotEmpty() || fallingBrick.z.first == 0
  22.                 if (supported) {
  23.                     supportingBricks.forEach {
  24.                         it.addOnTop(fallingBrick)
  25.                         fallingBrick.addSupport(it)
  26.                     }
  27.                     fallenBricks += fallingBrick
  28.                 } else {
  29.                     val nextZ =
  30.                         fallenBricks.filter { it.z.last < fallingBrick.z.first - 1 }
  31.                             .maxBy { it.z.last }?.z?.last?.let { it + 1 } ?: 0
  32.                     val brickHeight = fallingBrick.z.last - fallingBrick.z.first
  33.                     fallingBrick = Brick(
  34.                         fallingBrick.x, fallingBrick.y, nextZ .. nextZ + brickHeight
  35.                     )
  36.                 }
  37.             }
  38.         }
  39. //        fallenBricks.forEach {
  40. //            println("${it.supportedBy} <- [[$it]] -> ${it.supports}")
  41. //        }
  42.         brickStack = fallenBricks
  43.     }
  44.  
  45.     fun findBricksToTake(): List<Brick> =
  46.         brickStack.filter { brickToTake -> !brickToTake.supports.any { it.supportedBy.size == 1 } }
  47.  
  48.     fun howManyWillFall(): Int {
  49.         val bricksWithEffect = brickStack.filter { brickToTake -> brickToTake.supports.any { it.supportedBy.size == 1 } }
  50.         return bricksWithEffect.sumBy { takenBrick ->
  51.             val collapsingStack = brickStack.toMutableList()
  52.             collapsingStack.remove(takenBrick)
  53.             val bricksGone = mutableListOf(takenBrick)
  54.             do {
  55.                 val unsupported = collapsingStack.filter { it.supportedBy.isNotEmpty() && it.supportedBy.all { support -> bricksGone.contains(support) } }
  56.                 collapsingStack.removeAll(unsupported)
  57.                 bricksGone.addAll(unsupported)
  58.             } while (unsupported.isNotEmpty())
  59.             bricksGone.size - 1
  60.         }
  61.     }
  62. }
  63.  
  64. class Brick(val x: IntRange, val y: IntRange, val z: IntRange) {
  65.     val supports = mutableListOf<Brick>()
  66.     val supportedBy = mutableListOf<Brick>()
  67.  
  68.     fun addOnTop(other: Brick) {
  69.         supports += other
  70.     }
  71.  
  72.     fun addSupport(other: Brick) {
  73.         supportedBy += other
  74.     }
  75.  
  76.     override fun toString(): String {
  77.         return "[$x,$y,$z]"
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement