Skip to content

Instantly share code, notes, and snippets.

@Stvad
Created August 16, 2018 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Stvad/0cdc3d1cc06426bab81c29e1010b0569 to your computer and use it in GitHub Desktop.
Save Stvad/0cdc3d1cc06426bab81c29e1010b0569 to your computer and use it in GitHub Desktop.
Functional cubs Kotlin
object Part1 {
fun captcha(input: String): Int =
input.zipWithNext()
.let { it + (input.first() to input.last()) }
.filter { (first, second) -> first == second}
.map { it.first }
.map(Character::getNumericValue)
.sum()
}
object Part2 {
fun captcha(input: String): Int =
input.mapIndexed { index, c -> c to input[getIndex(index, input)] }
.filter { (first, second) -> first == second }
.map { it.first }
.map(Character::getNumericValue)
.sum()
private fun getIndex(index: Int, input: String) = (index + input.length / 2) % input.length
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment