[백준] Every Second Counts (15080)(kotlin)

원본 문제

문제 설명

Meredith runs a taxi service called Ruber which offers rides to clients in small towns in western Pennsylvania. She wants to get every possible dime out of people who use her taxis, so her drivers charge a flat fee not per minute but per second. It’s important, therefore, to be able to determine the exact amount of elapsed time between the moment a client enters a cab until the moment they leave. Trying to write a program to do this has driven Meredith crazy (pun intended) so she’s come to you for some help.

입력

Input consists of two lines: the first contains the start time and the second contains the end time for a single taxi ride. Each time is of the form hh : mm : ss, giving the hour, minute and seconds. Meredith uses a 24 hour clock, with 0 : 0 : 0 representing 12 midnight and 23 : 59 : 59 representing one second before midnight. Note that the end time may have a value less than the start time value if the ride spans midnight (see the last sample test case for an example of this).

출력

Display the number of seconds between the two times. No cab ride will be equal to or longer than 24 hours.

테스트 케이스

입력 출력
10 : 0 : 0
11 : 0 : 0
3600
13 : 30 : 52
13 : 31 : 7
15
23 : 0 : 0
1 : 30 : 0
9000

문제 풀이1

import java.text.SimpleDateFormat
import java.util.concurrent.TimeUnit

fun main(args: Array<String>) {
    val sdf = SimpleDateFormat("HH/mm/ss")

    val now = sdf.parse(
        readLine()!!
            .replace(" ", "")
            .split(":")
            .joinToString("/") {
                if (it.toInt() < 10) "0${it}"
                else it
            }
    )

    val targetTime = sdf.parse(
        readLine()!!
            .replace(" ", "")
            .split(":")
            .joinToString("/") {
                if (it.toInt() < 10) "0${it}"
                else it
            }
    )

    val diff = TimeUnit.SECONDS.convert(targetTime.time - now.time, TimeUnit.MILLISECONDS)
    println(if (diff < 0) diff + 3600 * 24 else diff)
}