문제링크
import java.util.*
data class Truck(val weight: Int, var position: Int)
fun main() {
val bufferedReader = System.`in`.bufferedReader()
val bufferedWriter = System.out.bufferedWriter()
val (n, w, L) = bufferedReader
.readLine()
.split(" ")
.map { it.toInt() }
val trucks = bufferedReader
.readLine()
.split(" ")
.map { Truck(it.toInt(), 0) }
val bridge: Queue<Truck> = LinkedList()
var time = 0
var index = 0
do {
checkArrivedTruck(bridge, w)
val totalWeightOnBridge = bridge.sumOf { it.weight }
if ((index < trucks.size) && (totalWeightOnBridge + trucks[index].weight <= L)) {
bridge.add(trucks[index])
index++
}
moveTrucksOnTheBridge(bridge)
time++
} while (bridge.isNotEmpty())
bufferedWriter.write("$time")
bufferedReader.close()
bufferedWriter.close()
}
fun checkArrivedTruck(bridge: Queue<Truck>, w: Int) {
bridge.peek()?.let { truck ->
if (truck.position == w) {
bridge.poll()
}
}
}
fun moveTrucksOnTheBridge(bridge: Queue<Truck>) {
bridge.forEach { truck ->
truck.position++
}
}
주석 없는 코드를 만들기 위해 노력하는 개발자입니다.
혹시라도 의도가 분명하지 않아보이는 (이해가 되지 않는) 코드가 있으시다면 편하게 답변 달아주시면 정말 감사하겠습니다.