/
Resources.kt
25 lines (18 loc) 路 877 Bytes
/
Resources.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.clouddjr.advent2022
import java.io.File
import java.net.URI
object Resources {
fun resourceAsText(fileName: String): String =
File(fileName.toURI()).readText()
fun resourceAsListOfString(fileName: String): List<String> =
File(fileName.toURI()).readLines()
fun resourceAsListOfInt(fileName: String): List<Int> =
resourceAsListOfString(fileName).map { it.toInt() }
fun resourceAsListOfLong(fileName: String): List<Long> =
resourceAsListOfString(fileName).map { it.toLong() }
fun resourceAsString(fileName: String, delimiter: String = ""): String =
resourceAsListOfString(fileName).reduce { a, b -> "$a$delimiter$b" }
private fun String.toURI(): URI =
Resources.javaClass.classLoader.getResource(this)?.toURI()
?: throw IllegalArgumentException("Cannot find Resource: $this")
}