2023-11-17 10:37:33 +01:00
|
|
|
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()
|
|
|
|
|
2023-11-17 11:53:16 +01:00
|
|
|
fun listOfIntegerLists(input: List<String>): ArrayList<List<Int>> {
|
|
|
|
val output = ArrayList<List<Int>>()
|
|
|
|
var currList = ArrayList<Int>()
|
2023-11-25 12:30:33 +01:00
|
|
|
for (i in input) {
|
|
|
|
if (i.isEmpty()) {
|
2023-11-17 11:53:16 +01:00
|
|
|
output.add(currList)
|
2023-11-25 12:30:33 +01:00
|
|
|
currList = ArrayList()
|
2023-11-17 11:53:16 +01:00
|
|
|
} else {
|
|
|
|
currList.add(i.toInt())
|
|
|
|
}
|
|
|
|
}
|
2023-11-25 12:30:33 +01:00
|
|
|
if (currList.isNotEmpty()) {
|
2023-11-17 11:53:16 +01:00
|
|
|
output.add(currList)
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2023-12-02 06:29:27 +01:00
|
|
|
fun squareLinearizedOneDigitArray(input: List<String>): 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<String>): Array<IntArray> {
|
|
|
|
return Array(input.size) { i ->
|
|
|
|
with(input[i]) {
|
|
|
|
val a = IntArray(length)
|
|
|
|
for (c in indices) {
|
|
|
|
a[c] = get(c).digitToInt()
|
|
|
|
}
|
|
|
|
a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-17 10:37:33 +01:00
|
|
|
/**
|
|
|
|
* 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)
|