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)
}
댓글 0