adamratzman
12/06/2020, 5:32 AMMichael de Kaste
12/06/2020, 10:31 AMJoris PZ
12/06/2020, 12:20 PMsrc\commonMain\resources\day01.txt
and read its contents as text in my solutions. This isn't trivial in Kotlin multi-platform, as a.) there isn't a common file API, and b.) only the JVM provides standardized access to the resources directory.
To deal with this platform-dependent stuff, Kotlin MPP offers the `expect`/`actual` mechanism. In common code, I declare that each target platform (JVM, Node and native) should offer a readInput
function:
expect fun readInput(name: String): String
The, each platform can implement this in a platform-specific way:
JVM:
actual fun readInput(name: String) = {}::class.java.getResource(name).readText()
Since Node and native don't get access to the resources directory, I use an enviroment variable AOC_INPUT_DIR
to define the directory where they should look for the input files. Their implementations then become:
Node:
external fun require(name: String): dynamic
val fs = require("fs")
val path = require("path")
actual fun readInput(name: String): String {
val path = path.join(process.env.AOC_INPUT_DIR, name)
return fs.readFileSync(path, "utf8") as String
}
Native:
actual fun readInput(name: String): String {
val returnBuffer = StringBuilder()
val inputDir = getenv("AOC_INPUT_DIR")?.toKString() ?: ""
val file = fopen("""$inputDir\$name""", "r") ?: throw IllegalArgumentException("Cannot open input file $name")
try {
memScoped {
val readBufferLength = 64 * 1024
val buffer = allocArray<ByteVar>(readBufferLength)
var line = fgets(buffer, readBufferLength, file)?.toKString()
while (line != null) {
returnBuffer.append(line)
line = fgets(buffer, readBufferLength, file)?.toKString()
}
}
} finally {
fclose(file)
}
return returnBuffer.toString()
}
I'm not sure I'm actually going to use this, I don't really mind having the input in the source, but at least I know how to do it now 🙂adamratzman
12/07/2020, 2:54 AMKroppeb
12/07/2020, 5:25 AMDraget
12/07/2020, 6:30 AMNir
12/07/2020, 3:52 PMmickeelm
12/07/2020, 6:52 PMbjonnh
12/07/2020, 9:18 PMadamratzman
12/08/2020, 4:41 AMbjonnh
12/08/2020, 5:10 AMDavid Whittaker
12/08/2020, 5:18 AMNir
12/08/2020, 5:41 AMNir
12/08/2020, 5:41 AMNir
12/08/2020, 3:08 PMasad.awadia
12/08/2020, 5:30 PMasad.awadia
12/08/2020, 7:38 PMNir
12/08/2020, 8:01 PMJérôme Gully
12/08/2020, 9:07 PMadamratzman
12/09/2020, 4:38 AMasad.awadia
12/09/2020, 4:40 PMbjonnh
12/10/2020, 3:54 AMadamratzman
12/10/2020, 5:32 AMbjonnh
12/11/2020, 12:06 AMbjonnh
12/11/2020, 4:48 AMNir
12/11/2020, 5:21 AMEdgars
12/11/2020, 8:44 AMDraget
12/11/2020, 2:25 PMdestructed
😛adamratzman
12/12/2020, 5:54 AMkartikpatodi
12/12/2020, 10:41 AMkartikpatodi
12/12/2020, 10:41 AMandyb
12/12/2020, 10:44 AMkartikpatodi
12/12/2020, 10:47 AMJakub Gwóźdź
12/12/2020, 11:55 AMsession=53616c74.....
Cookie
)ephemient
12/12/2020, 12:37 PMmake
downloads all available inputs for the given YEAR=. skips any inputs that already exist or future inputsmake day25.txt
it'll wait until thenkartikpatodi
12/12/2020, 12:55 PM