// 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? { val s: String? = readString() if (s == null) return null val pat: Regex = Regex("\\s+") return s.split(pat) } fun readInts(): List? { val words: List? = readWords() if (words == null) return null var lst: ArrayList = 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) { 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}") } }