第一题

This commit is contained in:
SOR
2026-09-21 14:41:50 +08:00
commit d698151d11
24 changed files with 775 additions and 0 deletions
@@ -0,0 +1,5 @@
package io.github.sor2171.puzzles.annotation
@Target(AnnotationTarget.FILE)
@Retention(AnnotationRetention.SOURCE)
annotation class SkipCompile(val skip: Boolean = true)
@@ -0,0 +1,23 @@
package io.github.sor2171.puzzles.service
import io.github.sor2171.puzzles.utils.CompileDetector
class PuzzleNote(
className: String,
vararg notes: PuzzleNote? = arrayOf(null)
) {
val className = CompileDetector.getColoredName(className)
val notes = notes.filterNotNull()
override fun toString(): String {
val validNotes = notes
if (validNotes.isEmpty()) return className
val builder = StringBuilder(className)
builder.append("\n │")
for (note in validNotes) {
builder.append("\n ├ ").append(note)
}
return builder.toString()
}
}
@@ -0,0 +1,57 @@
package io.github.sor2171.puzzles.service
import kotlin.random.Random
import kotlin.reflect.KClass
class RandomGenerator(vararg keys: Pair<String, KClass<*>>) {
val variableMap = mutableMapOf<String, Any>()
init {
keys.forEach { (name, type) ->
variableMap[name] = when (type) {
String::class -> randomString()
Byte::class -> Random.nextInt(
Byte.MIN_VALUE.toInt(),
Byte.MAX_VALUE.toInt() + 1
).toByte()
Short::class -> Random.nextInt(
Short.MIN_VALUE.toInt(),
Short.MAX_VALUE.toInt() + 1
).toShort()
Int::class -> Random.nextInt()
Long::class -> Random.nextLong()
// [0.0, 1.0)
Float::class -> Random.nextFloat()
Double::class -> Random.nextDouble()
UByte::class -> Random.nextInt(0, UByte.MAX_VALUE.toInt() + 1).toUByte()
UShort::class -> Random.nextInt(0, UShort.MAX_VALUE.toInt() + 1).toUShort()
UInt::class -> Random.nextInt().toUInt()
ULong::class -> Random.nextLong().toULong()
else -> error("不支持的类型: ${type.qualifiedName}")
}
}
}
private fun randomString(length: Int = Random.nextInt(5, 16)): String {
val chars = ('a'..'z') + ('A'..'Z').toList() + ('0'..'9').toList()
return buildString(length) {
repeat(length) {
append(chars[Random.nextInt(chars.size)])
}
}
}
operator fun get(key: String) = variableMap[key]
operator fun set(key: String, value: Any) {
variableMap[key] = value
}
}
@@ -0,0 +1,44 @@
package io.github.sor2171.puzzles.utils
import io.github.sor2171.puzzles.service.PuzzleNote
object CatalogStructure {
enum class Classification {
ALPHABET,
DIVE,
HYPER,
EXTRA;
override fun toString(): String {
return super.toString().lowercase()
}
}
val puzzlesArray = arrayOf(
arrayOf(
"${Classification.ALPHABET}.We need a variable, oh, maybe many",
"${Classification.EXTRA}.More strange variables",
),
arrayOf(
"${Classification.ALPHABET}.You need a function, not a mathematical one",
)
)
val firstPuzzleNote: PuzzleNote
get() {
var lastNote: PuzzleNote? = null
for (i in puzzlesArray.indices.reversed()) {
val array = puzzlesArray[i]
val noteArray = Array(array.size - 1) { i ->
PuzzleNote(array[i + 1])
}
lastNote = PuzzleNote(
array[0],
*noteArray
)
}
return lastNote!!
}
fun printAllMessage() = println(firstPuzzleNote)
}
@@ -0,0 +1,24 @@
package io.github.sor2171.puzzles.utils
object CompileDetector {
const val ANSI_GREEN = "\u001b[32m"
const val ANSI_RESET = "\u001b[0m"
fun detect(fullName: String): Boolean {
return try {
Class.forName(fullName)
true
} catch (_: ClassNotFoundException) {
false
}
}
fun fullName(className: String): String =
"io.github.sor2171.puzzles.$className"
fun getColoredName(className: String): String {
val fullName = fullName(className)
return if (detect(fullName)) "$ANSI_GREEN$className$ANSI_RESET"
else className
}
}
@@ -0,0 +1,14 @@
package io.github.sor2171.puzzles
import io.github.sor2171.puzzles.utils.CatalogStructure
import org.junit.jupiter.api.Test
/**
* 运行该测试类即可查询此 Kotlin Puzzle 的详细信息。
*/
class Catalog {
@Test
fun getCatalog() {
CatalogStructure.printAllMessage()
}
}
@@ -0,0 +1,23 @@
@file:SkipCompile(skip = true)
@file:Suppress("ClassName", "JoinDeclarationAndAssignment")
package io.github.sor2171.puzzles.alphabet
import io.github.sor2171.puzzles.annotation.SkipCompile
import io.github.sor2171.puzzles.service.RandomGenerator
import org.junit.jupiter.api.Test
class `We need a variable, oh, maybe many` {
@Test
fun puzzle() {
// write start 1
// write end 1
assert( a == 123)
b = "123"
println(a)
println(b)
}
}
@@ -0,0 +1 @@
io.github.sor2171.puzzles.extend.TestLifecycleLoggerExtension
@@ -0,0 +1 @@
junit.jupiter.extensions.autodetection.enabled=true