Skip to content

Instantly share code, notes, and snippets.

@Moire9
Created December 13, 2020 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Moire9/543695d814c09ebb36367a66c538904a to your computer and use it in GitHub Desktop.
Save Moire9/543695d814c09ebb36367a66c538904a to your computer and use it in GitHub Desktop.
Customizable Kotlin FizzBuzz program that uses CLI args
@file:Suppress("NOTHING_TO_INLINE")
inline fun e(i: Int): Nothing = throw IllegalArgumentException("Invalid FizzMap ($i)")
inline fun String.l() = toLongOrNull()
fun main(fm: Array<String>) = when {
fm.isEmpty() -> e(0)
fm.size % 2 == 1 -> Triple(1L, fm[fm.size-1].l() ?: e(1),2)
else -> Triple(fm[fm.size-2].l() ?: e(1), fm[fm.size-1].l() ?: e(1), 3)
}.apply {
val map = mapOf(*(fm.slice(0..fm.size - third).indices step 2).map { (fm[it].l() ?: e(2)) to fm[it+1] }.toTypedArray())
(first..second).map {
val sb = StringBuilder()
map.forEach { (x, y) -> if (it % x == 0L) sb.append(y) }
(if (sb.isEmpty()) it else sb).toString()
}.forEach(::println)
}.let{}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment