[프로그래머스] 개인정보 수집 유효기간(Kotlin)
풀이
class Solution {
fun dateAfterDur(date: String, dur:Int):String{
val dateList = date.split('.')
var y = dateList[0].toInt()
var m = dateList[1].toInt()
var d = dateList[2].toInt()
d += (dur*28 - 1)
while(d>28){
d-=28
m++
}
while(m > 12){
m -= 12
y++
}
return y.toString() + "." + m.toString() + "." + d.toString()
}
fun dateCompare(date1:String, date2:String):Boolean{
var dateList1 = date1.split('.')
var dateList2 = date2.split('.')
val y1 = dateList1[0].toInt()
val m1 = dateList1[1].toInt()
val d1 = dateList1[2].toInt()
val y2 = dateList2[0].toInt()
val m2 = dateList2[1].toInt()
val d2 = dateList2[2].toInt()
if(y1 < y2) return true
if(y1 > y2) return false
if(m1 < m2) return true
if(m1 > m2) return false
return d1 < d2
}
fun solution(today: String, terms: Array<String>, privacies: Array<String>): IntArray {
var termDuration = Array<Int>(26){0}
for(term in terms){
var t = term[0]
var dur = term.substring(2).toInt()
termDuration[t.code - 'A'.code] = dur.toInt()
}
var answer: IntArray = intArrayOf()
for((i, privacy) in privacies.withIndex()){
var privacyDate = privacy.substring(0,10)
var privacyTerm = privacy[11]
val privacyDateAfterDur = dateAfterDur(
privacyDate,
termDuration[privacyTerm.code - 'A'.code]
)
if(dateCompare(privacyDateAfterDur, today)) answer += (i+1)
}
return answer
}
}