import java.io.File import java.math.BigInteger import java.security.MessageDigest /** * Reads lines from the given input txt file. */ fun readInput(name: String) = File("src", "$name.txt") .readLines() fun listOfIntegerLists(input: List): ArrayList> { val output = ArrayList>() var currList = ArrayList() for (i in input) { if (i.isEmpty()) { output.add(currList) currList = ArrayList() } else { currList.add(i.toInt()) } } if (currList.isNotEmpty()) { output.add(currList) } return output } fun squareLinearizedOneDigitArray(input: List): IntArray { val dim = input.first().length val out = IntArray(dim * dim) for ((rnum, r) in input.withIndex()) { r.forEachIndexed { index, c -> out[rnum * dim + index] = c - '0' } } return out } fun twoDOneDigitArray(input: List): Array { return Array(input.size) { i -> with(input[i]) { val a = IntArray(length) for (c in indices) { a[c] = get(c).digitToInt() } a } } } /** * Converts string to md5 hash. */ fun String.md5() = BigInteger(1, MessageDigest.getInstance("MD5").digest(toByteArray())) .toString(16) .padStart(32, '0') /** * The cleaner shorthand for printing output. */ fun Any?.println() = println(this)