class Atbash {
    companion object {
        fun encode(inpString: String): String {
            return inpString.filter { it.isLetterOrDigit() }.map { it.atbash() }.joinToString(separator = "").splitIntoFives()
        }

        fun decode(inpString: String): String {
            return inpString.filter { it.isLetterOrDigit() }.map { it.atbash() }.joinToString(separator = "")
        }
    }
}

fun String.splitIntoFives():String {
    var outString = ""
    for (i in IntRange(0, this.length-1).step(5)) {
        for (j in 0..4) {
            outString += this.getOrElse(i + j, { ' ' })
        }
        outString += " "
    }
    return outString.trim()
}

fun Char.atbash(): Char{
    return when (this.toLowerCase()){
        'a' -> 'z'
        'b' -> 'y'
        'c' -> 'x'
        'd' -> 'w'
        'e' -> 'v'
        'f' -> 'u'
        'g' -> 't'
        'h' -> 's'
        'i' -> 'r'
        'j' -> 'q'
        'k' -> 'p'
        'l' -> 'o'
        'm' -> 'n'
        'n' -> 'm'
        'o' -> 'l'
        'p' -> 'k'
        'q' -> 'j'
        'r' -> 'i'
        's' -> 'h'
        't' -> 'g'
        'u' -> 'f'
        'v' -> 'e'
        'w' -> 'd'
        'x' -> 'c'
        'y' -> 'b'
        'z' -> 'a'
        else -> this.toLowerCase()
    }
}