Competitive Kotlin: Codesize

Helight
4 min readMar 18, 2022
Photo by Marc Reichelt on Unsplash

Kotlin is a great language with an even better standard library. Especially the collection and data conversion utilities are really helpful when trying to solve a problem as fast a possible, but when you have a task like CodinGame’s Clash of Code: Shortest category, you are mostly better off with not using Kotlin.

import java.util.*
import kotlin.math.*
fun main(){}

This simple application base alone occupies 52 chars, including newlines. In JS, you won’t have any of this, since both imports are part of the core library and main methods are not strictly required, making your code size smaller by nature.

Comparing code sizes language independently is generally flawed and unfair if you want to treat them equally.

If you still want use Kotlin for this type of competition because your just more comfortable with it or you are compared on a per-language level: Here are some tips for you.

Basics in Minifications

import java.util.*
import java.io.*
import java.math.*
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
fun
main(args : Array<String>) {
val input = Scanner(System.`in`)
}

This is the the boilerplate with which you start any CodingGame problem, excluding the problem specific Scanner reads. The simplest and most basic solution to minify this code is to replace the Scanner declaration with an ‘with’ and remove comments, empty lines and idents as well as the args parameter and the java.io.* import since you mostly likely won’t need it.

Generally try to remove anything which clarifies something.
Throw all your good patterns over board!

Applying those rules results in following boilerplate code:

import java.util.*
import java.math.*
fun main()=with(Scanner(System.`in`)){}

Variables

If you have any variable assignments, you can safely remove the spaces before and after the equal sign. Also consider replacing long variable names with simple single character names. The naming conventions you use for those single char variables is totally up to you. I personally have my own system I use for naming short variables as fast as possible while still giving them some meaning:

[a-h, t-w, o] General use variables
[i-k, r] Loop variables and indices
[l] (Single) List
[m] (Single) Map
[n,s] (Single) Length, Size value (I.e. “Get everything from X until n”)
[x-z] Temporary variables inside loops
[A-Z] Task specific variables/input

// 20 chars
val
test = nextInt()
// 15 chars
val
t=nextInt()

As you can see, even though you can reduce the size of variable declarations, the still require a delimiting semicolon or newline as well as whitespace between val/var and the variable name. The simplest variable assignment will in the end take up at least 7 chars, so try to avoid unnecessary variables where possible.

Arrays, Lists and Sets

Reading a fixed or length prepended array is a common part of many solutions and can, in its lengthy form look something like this:

// 66 chars
val
l=mutableListOf<Int>()
for(i in 0 until t){
l.add(nextInt())
}
// 26 chars
(0 until t).map{nextInt()}
// 18 chars
List(t){nextInt()}

Obviously this can be reduced simply by using map{} on the range, reducing it from 66 chars to 26 chars. But there is an even simpler solution: Using the initial size list constructor and its value initializer further reduces you code size to 18 chars.

Harness the stdlib

When trying to minimize the main part of your solution, refer to the possible solutions already mentioned and use stdlib extension functions where possible, since they are mostly the shortest option.

// 142 chars
import java.util.*
fun main()=with(Scanner(System.`in`)){var x = 0
for (i in 0 until nextLine().toInt()){
x+=nextLine().count()
}
println(x)
}
// 117 chars
import java.util.*
fun main()=with(Scanner(System.`in`)){println(List(nextLine().toInt()){nextLine().count()}.sum())}

Here for example we calculate the length sum for N string via two methods. The first method uses a normal for loop and a counter variable x and the other approach uses the list initializer and the sum extension. As you can see, even though the first solution is also minified, it still gets beaten by the second solution by 25 chars, which is a lot for such a simple task.

Conclusion

Kotlin is not optimal for creating the shortest (and most unreadable) code. Ruby, Perl do that way better and even most Scripting Languages like Python and JS are generally shorter. But as you can see, you can still minify your Kotlin code a lot to potentially win against other Kotlin or Java, C++ and C# users. And it really helps you learn more of the language and especially the stdlib.

I’m probably going to do some follow ups for this rich topic so stay tuned.

Further Reading

Reference

--

--