Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.72 KB | None | 0 0
  1. package pl.bartek.dungeons
  2.  
  3. import mu.KotlinLogging
  4. import kotlin.math.absoluteValue
  5.  
  6. const val CAGE_HEIGHT = 5
  7. val log = KotlinLogging.logger { }
  8.  
  9. fun main(args: Array<String>) {
  10.     val prison = Prison.load(args[0])
  11.     log.info { prison }
  12.  
  13.     val startCage = prison.prisonerCage
  14.     prison.cages[startCage.x][startCage.y].waterLevel = 1
  15.     prison.filledCages.add(startCage)
  16.     val output = fillWater(prison, startCage)
  17.  
  18.     log.info { "Result: " + prison.cages.flatten().map { it.waterLevel }.reduce { acc, i -> acc + i } }
  19. //    log.info { "\n" + prison.filledCages.joinToString(separator = "\n") }
  20.     log.info { "\n" + prison.cages.joinToString(separator = "\n") }
  21. }
  22.  
  23. fun fillWater(prison: Prison, startCagePoint: Point): Int {
  24.     val startCage = prison.cages[startCagePoint.x][startCagePoint.y]
  25.  
  26.     for (i in -1..1) {
  27.         for (j in -1..1) {
  28.             val isStartCage = i == 0 && j == 0
  29.             val neighbourCagePoint = Point(startCagePoint.x + i, startCagePoint.y + j)
  30.             val isInBounds = neighbourCagePoint.x < prison.maxPrisonWidth
  31.                     && neighbourCagePoint.y < prison.maxPrisonHeight
  32.                     && neighbourCagePoint.x >= 0
  33.                     && neighbourCagePoint.y >= 0 // start cage is in a corner, so the cage hasn't neighbours all around
  34.             if (isStartCage || !isInBounds) {
  35.                 continue
  36.             }
  37.  
  38.             val neighbourCage = prison.cages[neighbourCagePoint.x][neighbourCagePoint.y]
  39.  
  40.             if (canWaterSlop(startCage, neighbourCage)) {
  41.  
  42.                 var waterLevel: Int
  43.  
  44.                 val startAndNeighbourCageDifference = ((startCage.depth + CAGE_HEIGHT) - (neighbourCage.depth + CAGE_HEIGHT)).absoluteValue
  45.                 when {
  46.                     isNeighbourHigher(startCage, neighbourCage) -> {
  47.                         waterLevel = if (startCage.waterLevel - startAndNeighbourCageDifference > 5) 5 else startCage.waterLevel - startAndNeighbourCageDifference
  48.                         if (waterLevel <= 0) {
  49.                             waterLevel = -1
  50.                         }
  51.  
  52.                         if (waterLevel >= 0) {
  53.                             log.debug { "Higher. Slopping $waterLevel from $startCagePoint ($startCage) to $neighbourCagePoint ($neighbourCage)" }
  54.                         }
  55.  
  56.                     }
  57.                     isNeighbourLower(startCage, neighbourCage) -> {
  58.                         waterLevel = if (startAndNeighbourCageDifference + startCage.waterLevel > 5) 5 else startAndNeighbourCageDifference + startCage.waterLevel
  59.                         log.debug { "Lower. Slopping $waterLevel from $startCagePoint ($startCage) to $neighbourCagePoint ($neighbourCage)" }
  60.  
  61.                     }
  62.                     else -> { // neighbour is on the same height
  63.                         waterLevel = startCage.waterLevel
  64.                         log.debug { "Equal. Slopping $waterLevel from $startCagePoint ($startCage) to $neighbourCagePoint ($neighbourCage)" }
  65.  
  66.                     }
  67.                 }
  68.  
  69.                 if (waterLevel > 0 && (waterLevel > neighbourCage.waterLevel || !prison.filledCages.contains(neighbourCagePoint))) {
  70.  
  71.                     prison.filledCages.add(neighbourCagePoint)
  72.                     neighbourCage.waterLevel = waterLevel
  73.                     fillWater(prison, neighbourCagePoint)
  74.  
  75.                 }
  76.             }
  77.         }
  78.     }
  79.  
  80.     return 0
  81. }
  82.  
  83. fun isNeighbourHigher(from: Cage, to: Cage): Boolean {
  84.     return from.depth - to.depth > 0
  85. }
  86.  
  87. fun isNeighbourLower(from: Cage, to: Cage): Boolean {
  88.     return from.depth - to.depth < 0
  89. }
  90.  
  91. fun canWaterSlop(from: Cage, to: Cage): Boolean {
  92.     return (from.depth - to.depth).absoluteValue < CAGE_HEIGHT
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement