Skip to content

Commit eb89eb2

Browse files
committed
Update README
1 parent 57a0deb commit eb89eb2

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,45 @@
11
# Kotlin-AoC-API
2-
Kotlin API for pulling input data for a specific year/day for Advent of Code (https://adventofcode.com/)
2+
![JitPack](https://img.shields.io/jitpack/version/com.github.jsoberg/Kotlin-AoC-API) [![](https://jitci.com/gh/jsoberg/Kotlin-AoC-API/svg)](https://jitci.com/gh/jsoberg/Kotlin-AoC-API)
3+
4+
Simple Kotlin API for pulling input data for a specified year/day for :santa: [Advent of Code](https://adventofcode.com/) :christmas_tree: from network, and then caching if locally desired.
5+
6+
## Setup
7+
8+
This can be added to a [Kotlin Advent of Code template repository](https://github.com/kotlin-hands-on/advent-of-code-kotlin-template) or any other Kotlin JVM project through [Jitpack](https://jitpack.io/#jsoberg/Kotlin-AoC-API).
9+
10+
Example usage which will cache input for each year/day combination in the `input` folder of the repository's root directory:
11+
```kotlin
12+
import com.soberg.kotlin.aoc.api.AdventOfCodeInputApi
13+
14+
fun readInput(year: Int, day: Int) = AdventOfCodeInputApi(
15+
cachingStrategy = AdventOfCodeInputApi.CachingStrategy.LocalTextFile("input")
16+
).blockingReadInput(
17+
year = year, // e.g. 2015-2024
18+
day = day, // e.g. 1-24
19+
sessionToken = sessionToken, // e.g. "21216c7...314d"
20+
).getOrThrow()
21+
```
22+
There are 3 available caching strategies:
23+
- `AdventOfCodeInputApi.CachingStrategy.None` - Performs no caching, reads from network every time.
24+
- `AdventOfCodeInputApi.CachingStrategy.LocalTextFile` - Caches to a local text file in `input/<year>/Day<day>.txt` from root, reading from this location instead of network on subsequent calls.
25+
- `AdventOfCodeInputApi.CachingStrategy.Custom` - Fully custom caching logic, if desired.
26+
27+
### Advent of Code Session Token
28+
29+
Note that input for Advent of Code is unique for each user - therefore, you need your unique session token in order to get your unique input. This will be in the `Cookie` header on the request made for an input page in Advent of Code (e.g. https://adventofcode.com/2023/day/1/input) and can be obtained in your browser. The header value is in the format `session=<token>`, where you'll want to copy the (non-human readable) `<token>` value; [this](https://github.com/GreenLightning/advent-of-code-downloader?tab=readme-ov-file#how-do-i-get-my-session-cookie) overview from [GreenLightning](https://github.com/GreenLightning) can help tofind the session token using various browser types.
30+
31+
**Your session token is a secret unique to your account, and should not be stored in git**. For the simple use case of a [Kotlin Advent of Code template repository](https://github.com/kotlin-hands-on/advent-of-code-kotlin-template), you can store the token value in a `session-token.secret` file of your root repository directory and read it in code like so:
32+
```kotlin
33+
import kotlin.io.path.Path
34+
import kotlin.io.path.exists
35+
import kotlin.io.path.readText
36+
37+
private fun readSessionToken(): String {
38+
val secretTokenFile = Path("session-token.secret")
39+
require(secretTokenFile.exists()) {
40+
"session-token.secret file must exist and contain the sessionToken for Advent of Code"
41+
}
42+
return secretTokenFile.readText().trim()
43+
}
44+
```
45+
**Remember to add the session-token.secret file to your `.gitignore` file**. Additionally, it's recommended to keep your unique user input private; if you're using storing cached input, make sure input locations are also specified in your `.gitignore` file.

0 commit comments

Comments
 (0)