Add gradle task to download input

This commit is contained in:
William Brawner 2022-12-02 22:55:47 -07:00
parent da07259e12
commit 17e4da2250

View file

@ -1,3 +1,6 @@
import java.net.URL
import java.net.HttpURLConnection
plugins { plugins {
kotlin("jvm") version "1.7.22" kotlin("jvm") version "1.7.22"
} }
@ -17,3 +20,52 @@ tasks {
gradleVersion = "7.6" gradleVersion = "7.6"
} }
} }
tasks.register("downloadInput") {
doLast {
val day: String = findProperty("day") as? String ?: error("day is required")
val challengeUrl = "https://adventofcode.com/2022/day/$day"
val inputUrl = "$challengeUrl/input"
exec {
commandLine("xdg-open", challengeUrl)
}
var sessionCookie = System.getenv("AOC_SESSION")
if (sessionCookie.isNullOrBlank()) {
error("AOC_SESSION environment variable missing or empty")
}
if (!sessionCookie.startsWith("session=")) {
sessionCookie = "session=$sessionCookie"
}
val url = URL(inputUrl)
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "GET"
connection.setRequestProperty("Cookie", sessionCookie)
val content = connection.inputStream.bufferedReader().readText()
val dayFileName = "Day${day.padStart(2, '0')}"
File("src", "${dayFileName}_test.txt").createNewFile()
File("src", "$dayFileName.txt").writeText(content)
val sourceFile = File("src", "$dayFileName.kt")
if (sourceFile.exists()) {
return@doLast
}
sourceFile.writeText("""
fun main() {
fun part1(input: List<String>): Int {
return 0
}
fun part2(input: List<String>): Int {
return 0
}
val testInput = readInput("${dayFileName}_test")
check(part1(testInput) == 0)
check(part2(testInput) == 0)
val input = readInput("$dayFileName")
println(part1(input))
println(part2(input))
}
""".trimIndent())
}
}