Created
March 1, 2017 18:56
-
-
Save l0rinc/0d1942f07788a6b3782b5338d30aa367 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.Math.* | |
data class Point(private val x_: Int, private val y_: Int) { | |
fun x() = max(0, min(x_, 2)) | |
fun y() = max(0, min(y_, 2)) | |
operator fun plus(p: Point) = Point(x() + p.x_, y() + p.y_) | |
override fun toString() = "${y() * 3 + x() + 1}" | |
} | |
private fun solve(input: String) = input.fold(mutableListOf(Point(1, 1)), { res, c -> | |
operator fun MutableList<Point>.plus(p: Point) = removeAt(size - 1) + p | |
res.add(when (c) { | |
'U' -> res + Point(0, -1) | |
'D' -> res + Point(0, 1) | |
'R' -> res + Point(1, 0) | |
'L' -> res + Point(-1, 0) | |
else -> res.last() | |
}) | |
res | |
}) | |
fun main(args: Array<String>) { | |
val input = """|ULL | |
|RRDDD | |
|LURDL | |
|UUUUD""".trimMargin() | |
println(solve(input)) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment