Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fun getInput(day: Int, year: Int = LocalDate.now().year, alternate: String = ""): String {
- val inputFile = File("src/main/kotlin/org/gristle/adventOfCode/y$year/d$day/input$alternate.txt")
- if (!inputFile.exists()) {
- if (alternate != "") throw IllegalArgumentException("Sample input specified but not provided!")
- inputFile.parentFile.mkdirs()
- val url = URL("https://adventofcode.com/$year/day/$day/input")
- val connection = url.openConnection() as HttpURLConnection
- connection.setRequestProperty("Cookie", "session=$SESSION")
- connection.setRequestProperty(
- "User-Agent",
- "github.com/nbanman/Play2022/blob/master/src/main/kotlin/org/gristle/adventOfCode/generate/Generate.kt by [email protected]"
- )
- try {
- connection.connect()
- check(connection.responseCode == 200) { "${connection.responseCode} ${connection.responseMessage}" }
- connection.inputStream.use { input ->
- inputFile.outputStream().use { output ->
- input.copyTo(output)
- }
- inputFile.setReadOnly()
- }
- } finally {
- connection.disconnect()
- }
- }
- return inputFile.readText().stripCarriageReturns().trimEnd { it == '\n' }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement