Created
August 16, 2018 21:31
-
-
Save Stvad/0cdc3d1cc06426bab81c29e1010b0569 to your computer and use it in GitHub Desktop.
Functional cubs Kotlin
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
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