[백준] Sounds fishy! (6764)(kotlin)

원본 문제

문제 설명

A fish-finder is a device used by anglers to find fish in a lake. If the fish-finder finds a fish, it will sound an alarm. It uses depth readings to determine whether to sound an alarm. For our purposes, the fish-finder will decide that a fish is swimming past if:

  • there are four consecutive depth readings which form a strictly increasing sequence (such as 3 4 7 9) (which we will call “Fish Rising”), or
  • there are four consecutive depth readings which form a strictly decreasing sequence (such as 9 6 5 2) (which we will call “Fish Diving”), or
  • there are four consecutive depth readings which are identical (which we will call “Constant Depth”). All other readings will be considered random noise or debris, which we will call “No Fish.”

Your task is to read a sequence of depth readings and determine if the alarm will sound.

입력

The input will be four positive integers, representing the depth readings. Each integer will be on its own line of input.

출력

The output is one of four possibilities. If the depth readings are increasing, then the output should be “Fish Rising”. If the depth readings are decreasing, then the output should be “Fish Diving”. If the depth readings are identical, then the output should be “Fish At Constant Depth”. Otherwise, the output should be “No Fish”.

테스트 케이스

입력 출력
1
10
12
13
Fish Rising

문제 풀이1

import java.util.Scanner
import java.util.LinkedList


fun main(args: Array<String>) = println(
    (0..3).asIterable()
        .map { sc.nextInt() }
        .let {
            val ff = fishFinder(it)
            when {
                ff[0] -> "Fish Rising"
                ff[1] -> "Fish Diving"
                ff[2] -> "Fish At Constant Depth"
                else -> "No Fish"
            }
        }
)

val sc = Scanner(System.`in`)

fun fishFinder(data: List<Int>): BooleanArray {
    var isRising = false
    var isDiving = false
    var isConstant = false
    val queue = LinkedList<Int>(data)

    var q = queue.poll()

    while (queue.isNotEmpty()) {
        val next = queue.poll()
        when {
            q < next -> {
                if (isDiving || isConstant) {
                    return booleanArrayOf(false, false, false)
                } else {
                    isRising = true
                }
            }
            q > next -> {
                if (isRising || isConstant) {
                    return booleanArrayOf(false, false, false)
                } else {
                    isDiving = true
                }
            }
            else -> {
                if (isRising || isDiving) {
                    return booleanArrayOf(false, false, false)
                } else {
                    isConstant = true
                }
            }
        }
        q = next
    }

    return booleanArrayOf(isRising, isDiving, isConstant)
}