Skip to content

Advent of Code template and interactive framework for Kotlin Notebook

License

Notifications You must be signed in to change notification settings

Toldoven/aoc-kotlin-notebook

Repository files navigation

Maven Central License

Advent of Code Kotlin Notebook Framework

Solve Advent of Code interactively, without leaving your IDE, with Kotlin Notebook!

aocKotlinNotebook.mp4

Prerequisites

To use Kotlin Notebook you need to have IntelliJ IDEA Ultimate (paid version).

You can start a trial for 30 days, or use Early Access version for free (not always available).

Install Kotlin Notebook Plugin.

Setup

1. Create a new Kotlin Notebook

2. Add a dependency

Just type this inside of the Kotlin Notebook:

@file:DependsOn("com.toldoven.aoc:aoc-kotlin-notebook:1.1.2")

Or with Gradle inside of build.gradle.kts if you don't want to add it manually every time you create a new notebook:

dependencies {
    implementation("com.toldoven.aoc:aoc-kotlin-notebook:1.1.2")
}

Don't forget to restart the Notebook kernel after adding a new dependency with Gradle.

3. Get your Advent of Code token

Open the developer console on the Advent of Code website and grab the session cookie.

4. Specify the token

Using an environment variable

Open Kotlin Notebook settings and set the environment variable AOC_TOKEN.

When constructing a client use AocClient.fromEnv().

Using a file

Create a file in the current working directory called .aocToken and paste your token inside.

When constructing a client use AocClient.fromFile().

You can specify a different path to the file with the AOC_TOKEN_FILE environment variable.

5. Update .gitignore

If you're uploading your solutions to GitHub — don't forget to add files with your token and cache to .gitignore.

Token

Caution

If you're using Kotlin Notebook environment variables, they are not ignored by default. They are saved in the .idea/kotlinNotebook.xml file.

Cache

All data that doesn't change is cached. This includes puzzle descriptions, input, and solutions.

The default cache folder is .aocCache in the current working directory. You can change it with AOC_CACHE_DIR environment variable.

Warning

Add cache folder to .gitignore. Redistributing Advent of Code puzzle descriptions and inputs is NOT ALLOWED.

Since input and solutions are unique for each user, they are stored in a folder named with a secure hashed session token.

Usage

Check out the example: example.ipynb

// Initialize a day
val aoc = AocClient.fromEnv().interactiveDay(2019, 1)

// Interactive timer synchronized with server time
aoc.waitUntilUnlocked()

// View part one problem description
aoc.viewPartOne()

// Get the input
aoc.input()

// Submit the first part
aoc.submitPartOne(123)

// View part two problem description
aoc.viewPartTwo()

// Submit the second part
aoc.submitPartOne("321")

Note

To display the value in the Notebook — it needs to be on the last line of the block, or you can use DISPLAY(...) function.

Compliance

This tool follows the automation guidelines on the /r/adventofcode community wiki.

  • Everything that can be cached is cached. (Source)
  • The calls to Advent of Code website are not throttled, because each call requires action from the user, and it's up to the user to use it responsibly.
  • The User-Agent header contains a link to this repo and contact info. (Source)

TODO

A few ideas I will hopefully get to implement.

  • A way to run tests.
    • Perhaps a way to extract tests from the description automatically with an LLM. Not sure if this is legal, though.
  • Improve integration using Kotlin Notebook's API.
  • A way to receive a notification when you can submit again after failing.

Additional Resources

About

Advent of Code template and interactive framework for Kotlin Notebook

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published