class Solution {


    fun solution(assets: Array<String>): Array<String> =

        assets.mapNotNull { asset -> runCatching { asset.toAssetResult() }.getOrNull() }

            .sorted()

            .filter(Asset::isValid)

            .map(Asset::text)

            .distinct()

            .toTypedArray()


}


object Validati : Throwable()



fun String.toAssetResult(): Asset =

    when {

        length != 9 -> throw Validati

        slice(0..1).toIntOrNull() == null -> throw Validati

        get(2) != '-' -> throw Validati

        runCatching { Asset.Type.valueOf(slice(3..4)) }.isFailure -> throw Validati

        slice(5..6).toIntOrNull() == null -> throw Validati

        slice(7..8).toIntOrNull() == null -> throw Validati

        else -> Asset(

            text = this,

            yy = slice(0..1).toInt(),

            type = Asset.Type.valueOf(slice(3..4)),

            mm = slice(5..6).toInt(),

            no = slice(7..8).toInt()

        )

    }


data class Asset(

    val text: String,

    val yy: Int,

    val type: Type,

    val mm: Int,

    val no: Int

) : Comparable<Asset> {


    enum class Type {

        SP, KE, MO, CO, DE

    }


    val isValid: Boolean

        get() {

            return (yy in 13..22) && (mm in 1..12) && when {

                yy == 13 && mm < 4 -> false

                yy == 22 && mm > 8 -> false

                else -> true

            } && (no in 1..99)

        }


    override fun compareTo(other: Asset): Int {

        return when {

            yy > other.yy -> 1

            yy < other.yy -> -1

            type.ordinal > other.type.ordinal -> 1

            type.ordinal < other.type.ordinal -> -1

            mm > other.mm -> 1

            mm < other.mm -> -1

            no > other.no -> 1

            no < other.no -> -1

            else -> 0

        }

    }


}


이 문제 말인가?


이것도


까놓고 말해서 코틀린 스타일이 아니잖아


코틀린 쓸거면


CompareTo(other:Asset) 이런식으로 쓰지



fun solution(assets: Array<String>): Array<String> =

    assets.mapNotNull { it.toAssetOrNull() }

        .filter { it.isValid() }

        .distinctBy { it.toString() }

        .sorted()

        .map { it.toString() }

        .toTypedArray()


data class Asset(val yy: Int, val type: Type, val mm: Int, val no: Int) : Comparable<Asset> {


    enum class Type {

        SP, KE, MO, CO, DE

    }


    override fun compareTo(other: Asset): Int =

        compareValuesBy(this, other, Asset::yy, Asset::type, Asset::mm, Asset::no)


    override fun toString(): String =

        "%02d-%s%02d%02d".format(yy, type.name, mm, no)


    fun isValid(): Boolean =

        yy in 13..22 &&

        mm in 1..12 &&

        no in 1..99 &&

        !(yy == 13 && mm < 4) &&

        !(yy == 22 && mm > 8)

}


fun String.toAssetOrNull(): Asset? {

    if (length != 9 || this[2] != '-') return null

    val yy = substring(0, 2).toIntOrNull() ?: return null

    val type = runCatching { Asset.Type.valueOf(substring(3, 5)) }.getOrNull() ?: return null

    val mm = substring(5, 7).toIntOrNull() ?: return null

    val no = substring(7, 9).toIntOrNull() ?: return null

    return Asset(yy, type, mm, no)

}


책임도 하나에 매몰되있고 걍 순전히 기능 구현만 넣은거던데

아래처럼 살짝만 품들이면 더 깔끔하게 만드는데 글쎄다 싶다

애초에 순서도 이상해

선 필터링-> 후 정렬이 되야하는데

왜 더 비용이 많이 드는 작업을 하고 있는거야

이게 모범 답안이라고 적어둔거보고 흠ㅋㅋ 싶던데