Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.bartek.dungeons
- import mu.KotlinLogging
- import kotlin.math.absoluteValue
- const val CAGE_HEIGHT = 5
- val log = KotlinLogging.logger { }
- fun main(args: Array<String>) {
- val prison = Prison.load(args[0])
- log.info { prison }
- val startCage = prison.prisonerCage
- prison.cages[startCage.x][startCage.y].waterLevel = 1
- prison.filledCages.add(startCage)
- val output = fillWater(prison, startCage)
- log.info { "Result: " + prison.cages.flatten().map { it.waterLevel }.reduce { acc, i -> acc + i } }
- // log.info { "\n" + prison.filledCages.joinToString(separator = "\n") }
- log.info { "\n" + prison.cages.joinToString(separator = "\n") }
- }
- fun fillWater(prison: Prison, startCagePoint: Point): Int {
- val startCage = prison.cages[startCagePoint.x][startCagePoint.y]
- for (i in -1..1) {
- for (j in -1..1) {
- val isStartCage = i == 0 && j == 0
- val neighbourCagePoint = Point(startCagePoint.x + i, startCagePoint.y + j)
- val isInBounds = neighbourCagePoint.x < prison.maxPrisonWidth
- && neighbourCagePoint.y < prison.maxPrisonHeight
- && neighbourCagePoint.x >= 0
- && neighbourCagePoint.y >= 0 // start cage is in a corner, so the cage hasn't neighbours all around
- if (isStartCage || !isInBounds) {
- continue
- }
- val neighbourCage = prison.cages[neighbourCagePoint.x][neighbourCagePoint.y]
- if (canWaterSlop(startCage, neighbourCage)) {
- var waterLevel: Int
- val startAndNeighbourCageDifference = ((startCage.depth + CAGE_HEIGHT) - (neighbourCage.depth + CAGE_HEIGHT)).absoluteValue
- when {
- isNeighbourHigher(startCage, neighbourCage) -> {
- waterLevel = if (startCage.waterLevel - startAndNeighbourCageDifference > 5) 5 else startCage.waterLevel - startAndNeighbourCageDifference
- if (waterLevel <= 0) {
- waterLevel = -1
- }
- if (waterLevel >= 0) {
- log.debug { "Higher. Slopping $waterLevel from $startCagePoint ($startCage) to $neighbourCagePoint ($neighbourCage)" }
- }
- }
- isNeighbourLower(startCage, neighbourCage) -> {
- waterLevel = if (startAndNeighbourCageDifference + startCage.waterLevel > 5) 5 else startAndNeighbourCageDifference + startCage.waterLevel
- log.debug { "Lower. Slopping $waterLevel from $startCagePoint ($startCage) to $neighbourCagePoint ($neighbourCage)" }
- }
- else -> { // neighbour is on the same height
- waterLevel = startCage.waterLevel
- log.debug { "Equal. Slopping $waterLevel from $startCagePoint ($startCage) to $neighbourCagePoint ($neighbourCage)" }
- }
- }
- if (waterLevel > 0 && (waterLevel > neighbourCage.waterLevel || !prison.filledCages.contains(neighbourCagePoint))) {
- prison.filledCages.add(neighbourCagePoint)
- neighbourCage.waterLevel = waterLevel
- fillWater(prison, neighbourCagePoint)
- }
- }
- }
- }
- return 0
- }
- fun isNeighbourHigher(from: Cage, to: Cage): Boolean {
- return from.depth - to.depth > 0
- }
- fun isNeighbourLower(from: Cage, to: Cage): Boolean {
- return from.depth - to.depth < 0
- }
- fun canWaterSlop(from: Cage, to: Cage): Boolean {
- return (from.depth - to.depth).absoluteValue < CAGE_HEIGHT
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement