Jump to content

Straddling checkerboard: Difference between revisions

Added Kotlin
m (→‎{{header|C#}}: use IReadOnlyDictionary)
(Added Kotlin)
Line 1,278:
Encoded: 125021250212707204372221327070218600960021823809623283724002424935226226962262521636094232328463769
Decoded: THECHECKERBOARDCAKERECIPESPECIFIES3LARGEEGGSAND2.25CUPSOFFLOUR.
</pre>
 
=={{header|Kotlin}}==
<lang scala>// version 1.2.0
 
val board = "ET AON RISBCDFGHJKLMPQ/UVWXYZ."
val digits = "0123456789"
val rows = " 26"
val escape = "62"
val key = "0452"
 
fun encrypt(message: String): String {
val msg = message.toUpperCase()
.filter { (it in board || it in digits) && it !in " /" }
val sb = StringBuilder()
for (c in msg) {
val idx = board.indexOf(c)
if (idx > -1) {
val row = idx / 10
val col = idx % 10
sb.append(if (row == 0) "$col" else "${rows[row]}$col")
}
else {
sb.append("$escape$c")
}
}
val enc = sb.toString().toCharArray()
for ((i, c) in enc.withIndex()) {
val k = key[i % 4] - '0'
if (k == 0) continue
val j = c - '0'
enc[i] = '0' + ((j + k) % 10)
}
return String(enc)
}
 
fun decrypt(encoded: String): String {
val enc = encoded.toCharArray()
for ((i, c) in enc.withIndex()) {
val k = key[i % 4] - '0'
if (k == 0) continue
val j = c - '0'
enc[i] = '0' + if (j >= k) (j - k) % 10 else (10 + j - k) % 10
}
val len = enc.size
val sb = StringBuilder()
var i = 0
while (i < len) {
val c = enc[i]
val idx = rows.indexOf(c)
if (idx == -1) {
val idx2 = c - '0'
sb.append(board[idx2])
i++
}
else if ("$c${enc[i + 1]}" == escape) {
sb.append(enc[i + 2])
i += 3
}
else {
val idx2 = idx * 10 + (enc[i + 1] - '0')
sb.append(board[idx2])
i += 2
}
}
return sb.toString()
}
 
fun main(args: Array<String>) {
val messages = listOf(
"Attack at dawn",
"One night-it was on the twentieth of March, 1888-I was returning",
"In the winter 1965/we were hungry/just barely alive",
"you have put on 7.5 pounds since I saw you.",
"The checkerboard cake recipe specifies 3 large eggs and 2.25 cups of flour."
)
for (message in messages) {
val encrypted = encrypt(message)
val decrypted = decrypt(encrypted)
println("\nMessage : $message")
println("Encrypted : $encrypted")
println("Decrypted : $decrypted")
}
}</lang>
 
{{out}}
<pre>
Message : Attack at dawn
Encrypted : 3565257935743007
Decrypted : ATTACKATDAWN
 
Message : One night-it was on the twentieth of March, 1888-I was returning
Encrypted : 495786945533698149645468540384645875238925776668221480708005915367278976
Decrypted : ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING
 
Message : In the winter 1965/we were hungry/just barely alive
Encrypted : 89645417896270736648201450026959060839767024608116557470618482160
Decrypted : INTHEWINTER1965WEWEREHUNGRYJUSTBARELYALIVE
 
Message : you have put on 7.5 pounds since I saw you.
Encrypted : 61983605685800834914704829124087264189730245691940889
Decrypted : YOUHAVEPUTON7.5POUNDSSINCEISAWYOU.
 
Message : The checkerboard cake recipe specifies 3 large eggs and 2.25 cups of flour.
Encrypted : 160225770664742408892673362274738052905225343251668487244454469139746678907466041088039436848815704
Decrypted : THECHECKERBOARDCAKERECIPESPECIFIES3LARGEEGGSAND2.25CUPSOFFLOUR.
</pre>
 
9,485

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.