added input downloader
This commit is contained in:
@@ -1,36 +1,42 @@
|
||||
package day01
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertIs
|
||||
import util.InputDownloader
|
||||
|
||||
class Day01Test {
|
||||
@Test fun part1() {
|
||||
val day = Day01()
|
||||
val test = """3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3"""
|
||||
val day = 1
|
||||
|
||||
val output = day.part1(test)
|
||||
println(output)
|
||||
assert(output == 11)
|
||||
val input = InputDownloader().getInput(day)
|
||||
val example = InputDownloader().getExample(day)
|
||||
|
||||
@Test fun part1Example() {
|
||||
part1(example, 11)
|
||||
}
|
||||
|
||||
@Test fun part2() {
|
||||
@Test fun part2Example() {
|
||||
part2(example, 31)
|
||||
}
|
||||
|
||||
@Test fun part1Solution() {
|
||||
part1(input, 1189304)
|
||||
}
|
||||
|
||||
@Test fun part2Solution() {
|
||||
part2(input, 24349736)
|
||||
}
|
||||
|
||||
|
||||
fun part1(input: String, expected: Int) {
|
||||
val day = Day01()
|
||||
val output = day.part1(input)
|
||||
println("output: $output")
|
||||
assert(output == expected)
|
||||
}
|
||||
|
||||
val test = """3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3"""
|
||||
|
||||
val output = day.part2(test)
|
||||
println(output)
|
||||
assert(output == 31)
|
||||
fun part2(input: String, expected: Int) {
|
||||
val day = Day01()
|
||||
val output = day.part2(input)
|
||||
println("output: $output")
|
||||
assert(output == expected)
|
||||
}
|
||||
}
|
||||
|
64
app/src/test/kotlin/util/InputDownloader.kt
Normal file
64
app/src/test/kotlin/util/InputDownloader.kt
Normal file
@@ -0,0 +1,64 @@
|
||||
package util
|
||||
|
||||
import java.net.URI
|
||||
import java.net.http.HttpClient
|
||||
import java.net.http.HttpRequest
|
||||
import java.net.http.HttpResponse
|
||||
import java.net.HttpCookie
|
||||
import java.net.CookieHandler
|
||||
import java.net.CookieManager
|
||||
import java.net.ConnectException
|
||||
import java.time.Duration
|
||||
import io.github.cdimascio.dotenv.dotenv
|
||||
|
||||
class InputDownloader {
|
||||
fun getInput(day: Int): String {
|
||||
val dotenv = dotenv()
|
||||
val sessionCookie = HttpCookie("session", dotenv["AOC_TOKEN"]);
|
||||
sessionCookie.path = "/"
|
||||
sessionCookie.version = 0
|
||||
|
||||
val manager = CookieManager()
|
||||
manager.getCookieStore().add(URI("https://adventofcode.com"), sessionCookie)
|
||||
|
||||
val client = HttpClient.newBuilder()
|
||||
.cookieHandler(manager)
|
||||
.connectTimeout(Duration.ofSeconds(10))
|
||||
.build()
|
||||
|
||||
val request = HttpRequest.newBuilder()
|
||||
.uri(URI.create("https://adventofcode.com/2024/day/$day/input"))
|
||||
.GET()
|
||||
.build()
|
||||
|
||||
val res = client.send(request, HttpResponse.BodyHandlers.ofString())
|
||||
if (res.statusCode() == 403) {
|
||||
throw ConnectException("Failed to download input for day $day. Is your session token correct?")
|
||||
}
|
||||
if (res.statusCode() != ) {
|
||||
throw ConnectException("Failed to download input for day $day. Is the day open yet?")
|
||||
}
|
||||
|
||||
return res.body().trim()
|
||||
}
|
||||
|
||||
fun getExample(day: Int): String {
|
||||
val client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build()
|
||||
|
||||
val req = HttpRequest.newBuilder()
|
||||
.uri(URI("https://adventofcode.com/2024/day/$day"))
|
||||
.GET()
|
||||
.build()
|
||||
|
||||
val res = client.send(req, HttpResponse.BodyHandlers.ofString())
|
||||
if (res.statusCode() != 200) {
|
||||
throw ConnectException("Failed to download example for day $day. Is the day open yet?")
|
||||
}
|
||||
|
||||
val bod = res.body()
|
||||
val start = bod.indexOf("<pre><code>")
|
||||
val stop = bod.indexOf("</code></pre>")
|
||||
val sliced = bod.substring(start + 11..<stop)
|
||||
return sliced.trim()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user