Skip to content

Instantly share code, notes, and snippets.

@plallin
Last active August 16, 2018 22:00
Show Gist options
  • Save plallin/caa78505397684fbdc80165b35c37b15 to your computer and use it in GitHub Desktop.
Save plallin/caa78505397684fbdc80165b35c37b15 to your computer and use it in GitHub Desktop.
My Solution for Functional Kubs' katas of August 2018
package functionalkubs
/**
* Implement part 1 of http://adventofcode.com/2017/day/1.
* See https://github.com/raphait/kotlin-functional-kubs for instructions.
*/
object Part1 {
fun captcha(input: String): Int {
return getArrayOfDoubleDigit(numberToArray(input)).sum()
}
fun numberToArray(numString: String): List<Int> {
return numString.split("").filter{!it.isBlank()}.map{it.toInt()}
}
fun getArrayOfDoubleDigit(originalArray: List<Int>): List<Int> {
return originalArray.filterIndexed { index, value -> originalArray[(index + 1) % originalArray.size] == value }
}
}
/**
* Implement part 2 of http://adventofcode.com/2017/day/1.
* See https://github.com/raphait/kotlin-functional-kubs for instructions.
*/
object Part2 {
fun captcha(input: String): Int {
return getArrayOfDoubleDigit(numberToArray(input)).sum()
}
fun numberToArray(numString: String): List<Int> {
return numString.split("").filter{!it.isBlank()}.map{it.toInt()}
}
fun getArrayOfDoubleDigit(originalArray: List<Int>): List<Int> {
return originalArray.filterIndexed { index, value -> originalArray[(index + (originalArray.size / 2)) % originalArray.size] == value }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment