Skip to content

Instantly share code, notes, and snippets.

@whaley
Created January 8, 2018 11:48
Show Gist options
  • Save whaley/1c94f250a9b3b0e0bc31c5a3af5407f4 to your computer and use it in GitHub Desktop.
Save whaley/1c94f250a9b3b0e0bc31c5a3af5407f4 to your computer and use it in GitHub Desktop.
fun captcha(input: String, stepping: Int = 1): Int {
val ints = input.map { Character.getNumericValue(it) }
fun calcValueAtStepping(currentIndex: Int) : Int {
val wrappedIndex = currentIndex + stepping
return if (wrappedIndex < ints.size - 1) ints[wrappedIndex] else ints[wrappedIndex % ints.size]
}
var sum = 0
for ((index, current) in ints.withIndex()) {
sum += if (current == calcValueAtStepping(index)) current else 0
}
return sum
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment