/** * A Dyslectic worst nightmare. It looks pretty easy, but to write down what you can do easily in your head not so * trivial. It turned out a bit more spaghetti if code statements then I liked, but I think this covers all cases. */ import kotlin.math.pow fun main() { val (x, y) = readln().split(" ", limit = 2).map { it.toDegrees()} when (x) { in 0.0..y -> y - x else -> x - y }.let { println( when (it) { in 0.0..180.0 -> it else -> 360.0 - it } ) } } fun String.toDegrees(): Double { return when (length) { 1 -> when (first()) { 'N' -> 0.0 'E' -> 90.0 'S' -> 180.0 'W' -> 270.0 else -> error("invalid") } 2 -> when (this) { "NE" -> 45.0 "SE" -> 135.0 "SW" -> 225.0 "NW" -> 315.0 else -> error("invalid") } else -> { val start = takeLast(2).toDegrees() dropLast(2).foldRightIndexed(start) { index, c, acc -> val diff = ((90.0 / (2.0.pow(length - 1 - index)))) acc + when { c == 'N' && start == 45.0 -> -diff c == 'E' && start == 135.0 -> -diff c == 'S' && start == 225.0 -> -diff c == 'W' && start == 315.0 -> -diff else -> diff } } } } }