Skip to content

Instantly share code, notes, and snippets.

@chusri
Forked from first087/MathCalc.kt
Created August 22, 2019 04:21
Show Gist options
  • Save chusri/27844fd4ab2996d17e36141a11d5b778 to your computer and use it in GitHub Desktop.
Save chusri/27844fd4ab2996d17e36141a11d5b778 to your computer and use it in GitHub Desktop.
โจทย์เลข ป.4
fun calc() {
val ans = mutableListOf<Double>()
do {
val random8 = mutableListOf<Int>()
for (i in 1..8) {
random8.add(Random.nextInt(0, 9 + 1))
}
fun charToRandomNumber(ch: Char) = random8[ch.toByte() - 65]
fun transformDigit(digit: Int, ch: Char) = 10.0.pow(digit - 1) * charToRandomNumber(ch)
fun strToNumber(str: String) = str.toList()
.mapIndexed { index, ch -> transformDigit(4 - index, ch) }
.sum()
val input = arrayListOf("AFBF", "CGHB", "DAFG", "AEAB")
val output = "BCDC"
val actual = strToNumber(input[0]) + strToNumber(input[1]) + strToNumber(input[2]) + strToNumber(input[3])
val expected = strToNumber(output)
val matched = actual == expected
if (matched && !ans.contains(actual)) {
ans.add(actual)
val a2h = random8.mapIndexed { index, i -> "${(index + 65).toChar()}=$i" }.joinToString(", ")
println("Ans#${ans.size} --> actual=$actual, expected=$expected [$a2h]")
}
} while (ans.size < 100)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment