Skip to content

Instantly share code, notes, and snippets.

@l0rinc
Created March 1, 2017 18:56
Show Gist options
  • Save l0rinc/0d1942f07788a6b3782b5338d30aa367 to your computer and use it in GitHub Desktop.
Save l0rinc/0d1942f07788a6b3782b5338d30aa367 to your computer and use it in GitHub Desktop.
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