Skip to content

Instantly share code, notes, and snippets.

@mknudsen
Last active Nov 2, 2015
Embed
What would you like to do?
fun main(args: Array<String>) {
// Starte mit "rats live on arona no evil star"
val sentence = "rats live on arona no evil star"
// 1. change all letters with their counterparts in the alphabet
val alphabetCounterparts: String = sentence.split(' ').map { word -> word.map { ch -> (122 - (ch.toInt() - 97)).toChar().toLowerCase() }.joinToString("") }.joinToString(" ")
// 2. convert all letters to their numeric value in the alphabet (a = 1)
val lettersToNumericValues:List<List<Int>> = alphabetCounterparts.split(' ').map { word -> word.map { ch -> ch.toInt() - 96 } }
// 3. add 1 to each number, repeat this step until the largest number is 99
val incrementedUntilOneIs99: List<List<Int>> = lettersToNumericValues.map { numberWord -> numberWord.map { it + (99 - (lettersToNumericValues.map { it.max() ?: 0 }.max() ?: 0)) } }
// 4. reverse the phrase but keep the order in the individual numbers
val reversed: List<List<Int>> = incrementedUntilOneIs99.reversed()
// 5. replace all numbers with their difference with 100
val substractedFrom100: List<List<Int>> = reversed.map { numberWord -> numberWord.map { 100 - it } }
// 6. convert all numbers to letters
val mappedBackToCharacters = substractedFrom100.map { numberWord -> numberWord.map { (it + 96).toChar() }.joinToString("") }.joinToString(" ")
System.out.println(mappedBackToCharacters)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment