// challenge 303 Easy Ricochet
// https://w...content-available-to-author-only...t.com/r/dailyprogrammer/comments/5vb1wf/20170221_challenge_303_easy_ricochet/
package challenge._303_easy
import java.util.*
fun readString(): String? {
val line: String? = readLine()
if (line == null) return null
return line.trim()
}
fun readWords(): List<String>? {
val s: String? = readString()
if (s == null) return null
val pat: Regex = Regex("\\s+")
return s.split(pat)
}
fun readInts(): List<Int>? {
val words: List<String>? = readWords()
if (words == null) return null
var lst: ArrayList<Int> = ArrayList()
for (word in words) {
try {
var v: Int = word.toInt()
lst.add(v)
}
catch(nfe: NumberFormatException) {
return null
}
}
return lst
}
enum class Corner {
UR, LR, LL, UL
}
data class Result
(val corner
: Corner
, val distance
: Int
, val bounces
: Int
, val
time: Int
)
class Game(H: Int, W: Int, V: Int) {
val H: Int = H + 1
val W: Int = W + 1
val V: Int = V
fun run(): Result {
var x = 0
var y = 0
var dx = 1
var dy = 1
var bounces = 0
var distance = 0
while (true) {
var bounced = false
if (x + dx < 0 || x + dx >= W) {
dx = -dx
bounced = true
}
if (y + dy < 0 || y + dy >= H) {
dy = -dy
bounced = true
}
if (bounced) bounces++
x += dx
y += dy
distance++
if (x == W-1 && y == H-1) {
return Result(Corner.LR, distance, bounces, distance / V)
} else if (x == W-1 && y == 0) {
return Result(Corner.UR, distance, bounces, distance / V)
} else if (x == 0 && y == H-1) {
return Result(Corner.LL, distance, bounces, distance / V)
} else if (x == 0 && y == 0) {
return Result(Corner.UL, distance, bounces, distance / V)
}
}
}
}
fun main(args: Array<String>) {
while (true) {
val HWV = readInts()
if (HWV == null || HWV.size == 0) {
println("done.")
break
}
val H = HWV[0]
val W = HWV[1]
val V = HWV[2]
if (H<1 || W<1 || V<1) {
println("done.")
break
}
val game = Game(H, W, V)
val r = game.run()
println("${r.corner} ${r.bounces} ${r.time}")
}
}
Ly8gY2hhbGxlbmdlIDMwMyBFYXN5IFJpY29jaGV0Ci8vIGh0dHBzOi8vdy4uLmNvbnRlbnQtYXZhaWxhYmxlLXRvLW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dWx0KHZhbCBjb3JuZXI6IENvcm5lciwgdmFsIGRpc3RhbmNlOiBJbnQsIHZhbCBib3VuY2VzOiBJbnQsIHZhbCB0aW1lOiBJbnQpCgpjbGFzcyBHYW1lKEg6IEludCwgVzogSW50LCBWOiBJbnQpIHsKICAgIHZhbCBIOiBJbnQgPSBIICsgMQogICAgdmFsIFc6IEludCA9IFcgKyAxCiAgICB2YWwgVjogSW50ID0gVgogICAgZnVuIHJ1bigpOiBSZXN1bHQgewogICAgICAgIHZhciB4ID0gMAogICAgICAgIHZhciB5ID0gMAogICAgICAgIHZhciBkeCA9IDEKICAgICAgICB2YXIgZHkgPSAxCiAgICAgICAgdmFyIGJvdW5jZXMgPSAwCiAgICAgICAgdmFyIGRpc3RhbmNlID0gMAogICAgICAgIHdoaWxlICh0cnVlKSB7CiAgICAgICAgICAgIHZhciBib3VuY2VkID0gZmFsc2UKICAgICAgICAgICAgaWYgKHggKyBkeCA8IDAgfHwgeCArIGR4ID49IFcpIHsKICAgICAgICAgICAgICAgIGR4ID0gLWR4CiAgICAgICAgICAgICAgICBib3VuY2VkID0gdHJ1ZQogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmICh5ICsgZHkgPCAwIHx8IHkgKyBkeSA+PSBIKSB7CiAgICAgICAgICAgICAgICBkeSA9IC1keQogICAgICAgICAgICAgICAgYm91bmNlZCA9IHRydWUKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoYm91bmNlZCkgYm91bmNlcysrCiAgICAgICAgICAgIHggKz0gZHgKICAgICAgICAgICAgeSArPSBkeQogICAgICAgICAgICBkaXN0YW5jZSsrCiAgICAgICAgICAgIGlmICh4ID09IFctMSAmJiB5ID09IEgtMSkgewogICAgICAgICAgICAgICAgcmV0dXJuIFJlc3VsdChDb3JuZXIuTFIsIGRpc3RhbmNlLCBib3VuY2VzLCBkaXN0YW5jZSAvIFYpCiAgICAgICAgICAgIH0gZWxzZSBpZiAoeCA9PSBXLTEgJiYgeSA9PSAwKSB7CiAgICAgICAgICAgICAgICByZXR1cm4gUmVzdWx0KENvcm5lci5VUiwgZGlzdGFuY2UsIGJvdW5jZXMsIGRpc3RhbmNlIC8gVikKICAgICAgICAgICAgfSBlbHNlIGlmICh4ID09IDAgJiYgeSA9PSBILTEpIHsKICAgICAgICAgICAgICAgIHJldHVybiBSZXN1bHQoQ29ybmVyLkxMLCBkaXN0YW5jZSwgYm91bmNlcywgZGlzdGFuY2UgLyBWKQogICAgICAgICAgICB9IGVsc2UgaWYgKHggPT0gMCAmJiB5ID09IDApIHsKICAgICAgICAgICAgICAgIHJldHVybiBSZXN1bHQoQ29ybmVyLlVMLCBkaXN0YW5jZSwgYm91bmNlcywgZGlzdGFuY2UgLyBWKQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9CgpmdW4gbWFpbihhcmdzOiBBcnJheTxTdHJpbmc+KSB7CiAgICB3aGlsZSAodHJ1ZSkgewogICAgICAgIHZhbCBIV1YgPSByZWFkSW50cygpCiAgICAgICAgaWYgKEhXViA9PSBudWxsIHx8IEhXVi5zaXplID09IDApIHsKICAgICAgICAgICAgcHJpbnRsbigiZG9uZS4iKQogICAgICAgICAgICBicmVhawogICAgICAgIH0KICAgICAgICB2YWwgSCA9IEhXVlswXQogICAgICAgIHZhbCBXID0gSFdWWzFdCiAgICAgICAgdmFsIFYgPSBIV1ZbMl0KICAgICAgICBpZiAoSDwxIHx8IFc8MSB8fCBWPDEpIHsKICAgICAgICAgICAgcHJpbnRsbigiZG9uZS4iKQogICAgICAgICAgICBicmVhawogICAgICAgIH0KICAgICAgICB2YWwgZ2FtZSA9IEdhbWUoSCwgVywgVikKICAgICAgICB2YWwgciA9IGdhbWUucnVuKCkKICAgICAgICBwcmludGxuKCIke3IuY29ybmVyfSAke3IuYm91bmNlc30gJHtyLnRpbWV9IikKICAgIH0KfQ==