Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2024
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.31 KB | None | 0 0
  1. fun getInput(day: Int, year: Int = LocalDate.now().year, alternate: String = ""): String {
  2.     val inputFile = File("src/main/kotlin/org/gristle/adventOfCode/y$year/d$day/input$alternate.txt")
  3.     if (!inputFile.exists()) {
  4.         if (alternate != "") throw IllegalArgumentException("Sample input specified but not provided!")
  5.         inputFile.parentFile.mkdirs()
  6.         val url = URL("https://adventofcode.com/$year/day/$day/input")
  7.         val connection = url.openConnection() as HttpURLConnection
  8.         connection.setRequestProperty("Cookie", "session=$SESSION")
  9.         connection.setRequestProperty(
  10.             "User-Agent",
  11.             "github.com/nbanman/Play2022/blob/master/src/main/kotlin/org/gristle/adventOfCode/generate/Generate.kt by [email protected]"
  12.         )
  13.         try {
  14.             connection.connect()
  15.             check(connection.responseCode == 200) { "${connection.responseCode} ${connection.responseMessage}" }
  16.             connection.inputStream.use { input ->
  17.                 inputFile.outputStream().use { output ->
  18.                     input.copyTo(output)
  19.                 }
  20.                 inputFile.setReadOnly()
  21.             }
  22.         } finally {
  23.             connection.disconnect()
  24.         }
  25.     }
  26.     return inputFile.readText().stripCarriageReturns().trimEnd { it == '\n' }
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement